View Single Post
  #5  
Old 10-18-2009, 12:08 AM
pfyon's Avatar
pfyon
Discordant
 
Join Date: Mar 2009
Location: Ottawa
Posts: 495
Default

Not a lot of experience with perl either, but you just want to know the length of the array? A quick google got me this blog posting with 3 ways to determine the number of elements in an array: http://www.devdaily.com/blog/post/pe...gth-perl-array .

Code:
A frequently asked question Perl question is "How do I determine the size of a Perl array?", or the equivalent "How do I determine how many elements are in a Perl array?"

There are at least three different ways to do determine the length of a Perl array. The easiest way to demonstrate this is with some sample source code:

#----------------------------#
# create a simple Perl array #
#----------------------------#
@foods = qw(burgers fries shakes);

#------------------------------------------------------#
# three different ways to get the size of a Perl array #
#------------------------------------------------------#

# 1) implicit scalar conversion:
$size1 = @foods;

# 2) explicit scalar conversion:
$size2 = scalar @foods;

# 3) index of the last element in the array, plus one:
$size3 = $#foods + 1;

printf("The sizes are %d, %d, and %d\n", $size1, $size2, $size3);

which has the corresponding output:

The sizes are 3, 3, and 3
Reply With Quote