LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   perl - get number of elements in an array (https://www.linuxquestions.org/questions/programming-9/perl-get-number-of-elements-in-an-array-298822/)

AM1SHFURN1TURE 03-07-2005 03:10 PM

perl - get number of elements in an array
 
is there any way to get the number of elements an array has?
say i have @array = ("one","two","three","four","five","six")
is there a possible way to get the number of how many elements there are from
$array[0..$#array] and then assign it to a variable?

david_ross 03-07-2005 03:23 PM

Just use:
$numelements=@array;

AM1SHFURN1TURE 03-07-2005 03:38 PM

oh goodness, had no idea all I had to do was assign some scalar to it
thanks for the reply!

acid_kewpie 03-07-2005 03:59 PM

in longhand you would explicitly request a scalar interpretation of it, e.g "$num = scalar @array". it's not needed, but does make it slightly clearer for when you can't be bothered to comment your own code.


All times are GMT -5. The time now is 01:25 AM.