mndoci has asked for the wisdom of the Perl Monks concerning the following question:

This stems out of a conversation a friend and I just had that left us guessing the answer. I am sure someone here will be able to enlighten us.

He is using foreach on an array. We were wondering if the commands in the loop are carried out on each element of the array sequentially (by index) or at random, or some other way. From "Programming Perl" I remember that with foreach there is no way of knowing where in a list one is. This seems to allude to a non-sequential process. Is this true?

cheers
mndoci

"What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'

Replies are listed 'Best First'.
Re: foreach and arrays
by jwest (Friar) on Apr 19, 2001 at 01:55 UTC
    foreach() itterates from one element to the successive element. In order for there to be a successor, there must be a semblance of order, so it's sequentially.

    Further, foreach() is a synonym for for(), and for
    for $index (0 .. $somenumber) { ... };
    to work as you expect, it has to be sequential.

    At least, that's to my understanding. :)

    Hope this helps!

    --jwest
    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
    
Re: foreach and arrays
by Sherlock (Deacon) on Apr 19, 2001 at 02:02 UTC
    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
      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--
      
Re: foreach and arrays
by Maclir (Curate) on Apr 19, 2001 at 02:20 UTC
    My understanding is that a foreach loop processes the array sequentially, just as a "standard" for loop would do.

    I think where the statement <quote>there is no way of knowing where in a list one is</quote> comes from is that there is no explicit loop variable - if you want to know where you are up to in the array, you have to keep track of that yourself. However, the foreach is generally processing a list, no necessarily an array. I could define the contents of the list explicitly, such as:

    print 'I like'; foreach ('spam', 'spam', 'spam', 'eggs', 'and', 'spam') { print " $_"; } print ".\n";
    and there is no way of knowing that, for example, when I am printing the work "eggs" that this is the fourth time through the loop.

    The result is, of course:

    $ spam.pl I like spam spam spam eggs and spam. $
Re: foreach and arrays
by Xxaxx (Monk) on Apr 19, 2001 at 05:46 UTC
    My guess is that you are dredging up a half remembered comment about a 'foreach' when applied to a hash.
    foreach my $key (keys %hashname) { ..... }
    In most texts on Perl you will see some reference to the fact that keys will return the keys to the hash but "not in any definite order."

    I think this is what you were half remembering.

    Claude

Re: foreach and arrays
by rchiav (Deacon) on Apr 19, 2001 at 02:07 UTC
    Just because you don't know where you are in the array, doesn't mean that it isn't processed sequentially. I don't know for a fact that it is, but here's my logic.

    suppose you do a  foreach $element (@somearray). You're then working with $element which refrences something in @somearray. Now there's no way to tell what $element is refrencing in @somearray but that doesn't mean that the processing isn't sequential.

    Rich