Saturday, July 9, 2011

various method of array size in perl -- array length

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

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

# 1) perl array size with implicit scalar conversion:
$size1 = @foods;

# 2) perl array size with explicit scalar conversion:
$size2 = scalar @foods;

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

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

No comments: