On Tue, 17 Jul 2001, Evan Panagiotopoulos wrote:

> I have an array of X elements and I want to go from the first element
> (is it zero?) to the last. I have a variable containing the number of
> elements in the array. If the array has 22 elements does it go from 0
> to 21 or 1 to 22?

foreach doesn't need to know the number of elements - it just goes from
the first element to the last

e.g.

#!/usr/bin/perl -w
my @array = (1,2,3,4,5,6,7,8,9);

# explicit scalar variable
foreach my $item ( @array ) {
    print "$item";
}
# newline for tidiness
print "\n";

# do it again using the implicit $_ variable
foreach ( @array ) {
    print;
}
# newline for tidiness
print "\n";

This script prints out:
123456789
123456789


Best wishes,

Rachel Coleman



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to