Perl length function returns the number of characters of a string. You can’t use this function directly to find out the number of elements of an array or a hash because it expects as argument a scalar and it will force the array/hash into a scalar context. See Perl Arrays Size or Perl Arrays Length for more details about the size or the length of an array.But I anticipated a bit, for this function two syntax forms are available:
This function has only one argument – the string whose length we need to find. If you omit the argument, the Perl length function will return the length of the special variable $_.You can either omit the parentheses or not, sometimes it is healthy to put it in order to remove any ambiguity. In the below code sample we’ll show you how to use it.
Example 1.
# define a string variable
my $strVar = "A simple example of a string variable";
# get the length of the string
my $strLength = length($strVar);
# print the length
print "Our string has $strLength characters\n";
# it prints: Our string has 37 characters
Check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.
Example 2.Please note that Perl length function is for characters, so if you use a Unicode expression, it will return the number of characters and not the number of bytes. However, you can get the length of a string in bytes as you can see in the next snippet code:
# define a string variable
my $chars = "\x{2603}\x{2602}";
{
use bytes;
print "Length in bytes: ", length($chars), "\n";
}
print "Length in characters: ", length($chars), "\n";
In the above example:
- the string variable $chars contains two Unicode characters: 2603 (which is a snowman) and 2602 (an umbrella) and I used hexadecimal escapes to represent them. A hexadecimal escape begins with \x and it is either followed by two digit hexadecimal number (for example "\x31" is "1") or a hexadecimal of arbitrary length included in curly braces (\x{2603})
- I used bytes pragma to get the string length in bytes and I included it in a block in order to limit its effect to the scope of the block
- I printed the length of the string both in bytes and in characters
If we run this code we get the following output:
Length in bytes: 6
Length in characters: 2
Example 3.I’ll show you next a short code sample that reads some lines of text from STDIN. We use Perl length function to find the shortest and the longest string line:
my $minStrLength = "";
my $maxStrLength = "";
while(<STDIN>)
{
chomp;
if($minStrLength eq "")
{
$minStrLength = $maxStrLength = $_;
next;
}
if(length $_ < length $minStrLength)
{
$minStrLength = $_;
next;
}
if(length $_ > length $maxStrLength)
{
$maxStrLength = $_;
}
}
print "Str min: $minStrLength\n";
print "Str max: $maxStrLength\n";
We read the string lines from STDIN and we chomp off each line because the trailing newlines are also included in the length calculation. We store the shortest string line in the $minStrLength variable and the longest one in $maxStrLength variable. We don’t use any variable for the while loop, so the lines from STDIN will be stored in the special variable $_, which we use for comparisons. Finally, we print the wanted strings. See the code, copy and test it if you want.Example 4.
The last example will show you how to use the Perl length function in connection with arrays and sort function. We want to sort the words of a string in a descending length order and next print it. See the code for more details:
# create and initialize a string
my $str = "Here are a few Perl functions";
# split the string in an array
my @array = split(' ',$str);
# sort the array by descending value length
@array = sort { length $b cmp length $a } @array;
# convert back the array to string
$str = join ' ', @array;
# print the string
print "$str\n";
The above code will produce the following result:functions Here Perl are few a
Please click here to download the Perl length script with all the above examples included.