Forum Moderators: coopster & phranque

Message Too Old, No Replies

How do find the length of an array in perl?

         

cham3l3on35

3:27 am on Jun 5, 2002 (gmt 0)



I'm just learning perl, and i need to find a way to check to see if i'm at the last element in my array... how would i go about this?

mdharrold

3:30 am on Jun 5, 2002 (gmt 0)

10+ Year Member



scalar(@array) gives you the number of elements in the array.
Is that what you were wanted?

Brett_Tabke

6:29 am on Jun 5, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you can also do $#array.

$thelength =$#array;

That comes out as array-1 (counting zero).

I know that really breaks from perls standard syntax with the # char, but that's the way they do it.

scotty

10:05 am on Jun 5, 2002 (gmt 0)

10+ Year Member



> you can also do $#array... <snip>

That's probably coming from the bourn shell "heritage" where you look at the number of arguments using $#... Wait until Perl 6 comes out, and then everything will be different again. :(

Brett_Tabke

10:10 am on Jun 5, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you don't always have to use "scalar" you can do just

$t=@foo;