Mega Code Archive

 
Categories / Perl / System Functions
 

Using scalar() function to interpret an array variable in scalar context to get its length

$number = scalar( @array ); #!/usr/bin/perl -w # Two ways to get the length of an array. @array = (1,2,3,4,5,6,7,8,9); print "@array\n"; $number = $#array + 1; print "Number elements: $number.\n"; # Alternate method. $number = scalar( @array ); print "Alternate method: $number.\n";