http://www.perlmonks.org?node_id=299143


in reply to Re: How do I find the size of an array?
in thread How do I find the size of an array?

$#array is the index of the last element in @array. In this case, the # character does NOT introduce a comment.

For example, if you have @array = qw(ant bat cat dog eagle fish ghoti) then $#array is 6 (the index of 'ghoti'). The number of elements in the array is usually $#array + 1.

Note that those values will change if $[ is set to anything other than 0. To cope with those times when someone has been naughty (use of $[ is strongly discouraged), you should get the number of elements in an array by evaluating @array in scalar context, such as:

print scalar @array;
or
$num_elements = @array;