Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Interview Prepration

by merlyn (Sage)
on Apr 04, 2005 at 17:36 UTC ( [id://444742]=note: print w/replies, xml ) Need Help??


in reply to Re: Interview Prepration
in thread Interview Prepration

There's no difference between for and foreach.
Ahem. Not this again. Please see what I just said, elsewhere in this thread.

This meme must die. Even if I have to hunt down every appearance of it. {sigh}

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^3: Interview Prepration
by chromatic (Archbishop) on Apr 04, 2005 at 17:53 UTC

    That's a really thin line to walk -- thin enough that I think it's almost meaningless and definitely more confusing than useful. Is this a for loop?

    foreach ( my $i = 0; $i < 10 ; $i++ ) { print "\$i is $i\n"; }

    Is this a foreach loop?

    for my $user (@names) { print "Found user $user\n"; }
      Yes, and yes. Spelled "f-o-r", pronounced "foreach". Spelled "f-o-r-e-a-c-h", pronounced "for".

      If you start handwaving "for and foreach are the same" in front of people enough, they wonder why the variable "didn't localize in my for loop... I thought for did that!".

      It really is important to distinguish the two kinds of loops, even though 90% of the time we write "for", we prounounce it "foreach".

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Actually I think the community has selected a third way to avoid this debate, whatever it actually says in Perlsyn. The way they solved this debate is they say for LIST and 3 arg for loop or c-style for loop or if you are like me the while loop that likes to crossdress and call itself a for loop.

        I agree with chromatic, your usage, while strictly correct is confusing and misleading, especially when it leads you to obvious contortions like saying

        foreach(;1;) { }
        is a "for" loop and
        print for @list;
        is a "foreach" loop.

        Hopefully youll drop this piece of pedantry as its just confusing.

        ---
        demerphq

Re^3: Interview Prepration
by jZed (Prior) on Apr 04, 2005 at 18:02 UTC
    The difference between "for" and "foreach" is four characters of extra typing for the latter. Other than that they are the same. Because you apparently associate one of the words with a Perl-style loop and another with a C-style loop is not sufficient reason to say that "for" is different from "foreach". The words may have a different history, but their use in scripts is identical.
      That's a bit like saying the difference between George Bush and George W Bush is only the W. After all, you can use George Bush to refer to either one.

      But they really are different people! They're not the same!

      And that's the same with for/foreach. They are not the same! There's a for loop, and there's a foreach loop, and you can use the word "for" or "foreach" to refer to either one, but they are not the same.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        If you can show me a single instance in which the use of "for" instead of "foreach" makes a difference in code, I'll believe you.
          A reply falls below the community's threshold of quality. You may see it by logging in.
Re^3: Interview Prepration
by Anonymous Monk on Apr 05, 2005 at 08:48 UTC
    Your opinion has been noted. But perl doesn't care about your opinion, and will treat the keywords for and foreach to be the same. I'm just answering the interview questions - and I think that the interview is about probing my perl knowledge, and what I know about the opinion of some well known members of the Perl community.
      The question was "What is the difference between for and foreach?" not "What is the difference between the keywords for and foreach?" - it could also mean "What are the differences between the for and foreach loops?" (of which there are several notable differences which perl does not treat the same)

      It's an ambiguous question that should be followed by another question to determine whether it is about the keywords or the loops.

      This post has too many branches :)
      --------------
      "But what of all those sweet words you spoke in private?"
      "Oh that's just what we call pillow talk, baby, that's all."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://444742]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-19 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found