Forum Moderators: coopster & phranque

Message Too Old, No Replies

Counting elements in an array

         

Caley

6:13 pm on Jul 26, 2005 (gmt 0)

10+ Year Member



how would i go about returning the number of elements in an array.

so far im using the long winded

$numofelements = 0;
foreach $temp (@array)
{
$numofelements = $numofelements +1;
}

SeanW

6:51 pm on Jul 26, 2005 (gmt 0)

10+ Year Member



In scalar context @array *is* the number of elements.

print scalar @array;

or

print "Number of files is: " . @array; # need the string in front to force to scalar

Or, if you want the index of the last element (ie #elements - 1)

print $#array;

Sean