get the last element of an array in perl
February 2nd, 2007 by Lawrence David
to get the last element of an array in perl, use the length of the array as your index.
for instance, if my array is very creatively named “my_array,”
the length of the array is:
$#my_array
and it’s last element is
$my_array[$#my_array]
$my_array[-1] would me more easier …
thats the prefect trick man.!!!
“$my_array[-1] would me more easier …”
I would say that both serve a different need, even though they both get the same value.
I believe it is more save with the $#my_array in case you have an array with 1 or 0 entries