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

My fellow monks,

What is the difference between for and foreach (other than four letters?)
It is my understanding that they really tend to act similarly, for example:
print "hi\n" foreach (1..3); print "hi\n" for (1..3); print $_ for('hi','over','there'); print $_ foreach('hi','over','there');


Now I realize that there is always the original (and some would consider normal) purpose for the for loop:
for(my $i = 0;$i < 2;$i++)

But other than that, are they any different? Is there anything else that one is better suited for than the other?

Thanks, Regretfully, this has probably already been asked, however I've been having a lot of bad luck with the search function lately...



"Weird things happen, get used to it."

Flame ~ Lead Programmer: GMS

Replies are listed 'Best First'.
Re: for vs foreach?
by talexb (Chancellor) on May 07, 2002 at 04:16 UTC
    From the Camel, 3rd Edition, p.118: The foreach keyword is just a synonym for the for keyword, so you can use .. (them) .. interchangeably, whichever you think is more readable in a given situation.

    Thus, I would suggest foreach for members of an array, and for for a C style for loop.

    --t. alex

    "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny

Re: for vs foreach?
by educated_foo (Vicar) on May 07, 2002 at 05:34 UTC
    I find that "foreach" does wonders for readability in situations like this:
    sub foo { my $array = @_; foreach ($array) { do stuff; } # ... }
    Your mileage may vary, of course.

    /s

    Update: I guess the joke doesn't come across as well on screen.

    s/readability/job security/
      Maybe this is just an inadvertent mistake, but think about what would happen with foo('bad','llama'):
      sub foo { my $array = @_; foreach ($array) { print "$_\n"; } }
      Watch how you assign from an array, because you could end up in jail!
Re: for vs foreach?
by Flame (Deacon) on May 07, 2002 at 04:12 UTC
    Doh, great, now that I've posted it, I come up with the right words to search for... Sorry bout that, nevermind...



    "Weird things happen, get used to it."

    Flame ~ Lead Programmer: GMS

Re: for vs foreach?
by thunders (Priest) on May 13, 2002 at 20:50 UTC
    while for and foreach as keywords are interchangible, in my opinion, in perl, foreach-style loops are better for most list processing tasks.
    foreach my $thing(@things){ process($thing); }
    But sometimes it's more intuitive to use for(;;) when you are processing only, say, every third item in a list.
    for(my $i=0;$i < scalar(@things); $i += 3){ process($things[$i]); }
Re: for vs foreach?
by jsprat (Curate) on May 07, 2002 at 17:24 UTC
    What is the difference between for and foreach (other than four letters?)

    Nothing substantial. But that reminds me of one of my favorite Larry Wall quotes:

    "...And don't tell me there isn't one bit of difference between null and space, because that's exactly how much difference there is. :-)"

A reply falls below the community's threshold of quality. You may see it by logging in.