Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: Interview Prepration

by jZed (Prior)
on Apr 04, 2005 at 18:02 UTC ( #444747=note: print w/replies, xml ) Need Help??

in reply to Re^2: Interview Prepration
in thread Interview Prepration

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.

Replies are listed 'Best First'.
Re^4: Interview Prepration
by merlyn (Sage) on Apr 04, 2005 at 18:08 UTC
    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.
        Do you mean the word for exchanged for the word foreach? No. Those keywords are syntactically equivalent. And I don't disagree with that fact.

        Do you mean replace a for loop with a foreach loop, or vice versa? Certainly! They have different semantics!

        The keyword "for" is the same as the keyword "foreach". The "for" loop is different from the "foreach" loop. Do I need to keep repeating this? {sigh}

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

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2021-06-22 15:14 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (106 votes). Check out past polls.