Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find size of an array in Perl

Tags:

perl

People also ask

How do you find the size of an array in Perl?

Note: In Perl arrays, the size of an array is always equal to (maximum_index + 1) i.e. And you can find the maximum index of array by using $#array. So @array and scalar @array is always used to find the size of an array.

What is $# in Perl?

EDIT: from perldoc perlvar : $# is also used as sigil, which, when prepended on the name of an array, gives the index of the last element in that array. my @array = ("a", "b", "c"); my $last_index = $#array; # $last_index is 2 for my $i (0 .. $#array) { print "The value of index $i is $array[$i]\n"; }

What is the maximum size of an array in Perl?

In the Perl programming languages, the maximum number of elements an array can hold is relevant to the amount of memory available on the VM. Just for comparison, the maximum number of elements an array can hold in Java is 2,147,483,647. Why is Perl a unique language? Perl.

How do you find the index of an array in Perl?

The grep equivalent would be $idx = grep { $array[$_] eq 'whatever' and last } 0 ..


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.


First, the second is not equivalent to the other two. $#array returns the last index of the array, which is one less than the size of the array.

The other two are virtually the same. You are simply using two different means to create scalar context. It comes down to a question of readability.

I personally prefer the following:

say 0+@array;          # Represent @array as a number

I find it clearer than

say scalar(@array);    # Represent @array as a scalar

and

my $size = @array;
say $size;

The latter looks quite clear alone like this, but I find that the extra line takes away from clarity when part of other code. It's useful for teaching what @array does in scalar context, and maybe if you want to use $size more than once.


This gets the size by forcing the array into a scalar context, in which it is evaluated as its size:

print scalar @arr;

This is another way of forcing the array into a scalar context, since it's being assigned to a scalar variable:

my $arrSize = @arr;

This gets the index of the last element in the array, so it's actually the size minus 1 (assuming indexes start at 0, which is adjustable in Perl although doing so is usually a bad idea):

print $#arr;

This last one isn't really good to use for getting the array size. It would be useful if you just want to get the last element of the array:

my $lastElement = $arr[$#arr];

Also, as you can see here on Stack Overflow, this construct isn't handled correctly by most syntax highlighters...


To use the second way, add 1:

print $#arr + 1; # Second way to print array size

All three give the same result if we modify the second one a bit:

my @arr = (2, 4, 8, 10);

print "First result:\n";
print scalar @arr; 

print "\n\nSecond result:\n";
print $#arr + 1; # Shift numeration with +1 as it shows last index that starts with 0.

print "\n\nThird result:\n";
my $arrSize = @arr;
print $arrSize;