Posted By: Anonymous
I seem to have come across several different ways to find the size of an array. What is the difference between these three methods?
my @arr = (2); print scalar @arr; # First way to print array size print $#arr; # Second way to print array size my $arrSize = @arr; print $arrSize; # Third way to print array size
The first and third ways are the same: they evaluate an array in scalar context. I would consider this to be the standard way to get an array’s size.
The second way actually returns the last index of the array, which is not (usually) the same as the array size.