in reply to foreach and arrays

To answer this, execute this block of code to see what happens:

@myArray = (0,0,0,0,0); $myCounter = 1; foreach $item (@myArray) { $item = $myCounter; $myCounter++; } for ( $i = 0; $i < 5; $i++ ) { print "$myArray[$i],"; }
You should get the output "1,2,3,4,5,". This would indicate that the foreach operator work sequentially, as one would expect.

-Sherlock

Replies are listed 'Best First'.
(jeffa) Re: Re: foreach and arrays
by jeffa (Bishop) on Apr 19, 2001 at 03:28 UTC
    Just a pointer, in the time it takes you to type @myArray or $myCounter, you could just as easily type my @array or my $counter

    You do enjoy the benifits of lexically binded variables, don't you?

    Also, you can save some typing with:

    my @array = qw(0 0 0 0 0); # well, not much on this one, but . . . my @array = (0)x5; # pure evil
    and use Perl for loops like Perl, not like C!
    for (0..$#array) { print $array[$_],','; }

    jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--