How do I get the length of a string in Perl?

PerlString

Perl Problem Overview


What is the Perl equivalent of strlen()?

Perl Solutions


Solution 1 - Perl

length($string)

perldoc -f length

length EXPR length Returns the length in characters of the value of EXPR. If EXPR is omitted, returns length of $_. Note that this cannot be used on an entire array or hash to find out how many elements these have. For that, use "scalar @array" and "scalar keys %hash" respectively.

       Note the characters: if the EXPR is in Unicode, you will get the num-
       ber of characters, not the number of bytes.  To get the length in
       bytes, use "do { use bytes; length(EXPR) }", see bytes.

Solution 2 - Perl

Although 'length()' is the correct answer that should be used in any sane code, Abigail's length horror should be mentioned, if only for the sake of Perl lore.

Basically, the trick consists of using the return value of the catch-all transliteration operator:

print "foo" =~ y===c;   # prints 3

y///c replaces all characters with themselves (thanks to the complement option 'c'), and returns the number of character replaced (so, effectively, the length of the string).

Solution 3 - Perl

length($string)

Solution 4 - Perl

The length() function:

$string ='String Name';
$size=length($string);

Solution 5 - Perl

You shouldn't use this, since length($string) is simpler and more readable, but I came across some of these while looking through code and was confused, so in case anyone else does, these also get the length of a string:

my $length = map $_, $str =~ /(.)/gs;
my $length = () = $str =~ /(.)/gs;
my $length = split '', $str;

The first two work by using the global flag to match each character in the string, then using the returned list of matches in a scalar context to get the number of characters. The third works similarly by splitting on each character instead of regex-matching and using the resulting list in scalar context

Solution 6 - Perl

Not an answer but an observation: I'm running v5.30.2 built for darwin-2level, and I've noticed that if I don't chomp my input first (as with stdin), then calculate the length, the length of a single character is not 1, but 2.

Remember those newlines and other hidden characters that could be a factor.

And I would love to find Abigail's post, but alas it seems to have been consumed by the ether.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKipView Question on Stackoverflow
Solution 1 - PerlPaul TomblinView Answer on Stackoverflow
Solution 2 - PerlYanickView Answer on Stackoverflow
Solution 3 - PerlJDragoView Answer on Stackoverflow
Solution 4 - PerlRANA DINESHView Answer on Stackoverflow
Solution 5 - PerlpslessardView Answer on Stackoverflow
Solution 6 - PerlsqldougView Answer on Stackoverflow