http://www.perlmonks.org?node_id=743767


in reply to Re^4: For vs. Foreach
in thread For vs. Foreach

I can't believe you are so far out of touch.

If @a contains 10 million elements, the doing ++$a[$_] for 10 .. $#a -10; causes no extra allocation of memory.

But doing ++$_ for @a[ 10 .. $#a -10 ]; nearly triples memory consumption from 198MB to 460MB.

Given the volumes of data people are manipulating with perl, knowing the distinction between a loop that essentially just increments a scalar (a "counting loop") and a loop that constructs a temporary list consisting of several hundreds of megabytes, is valueable information.

As opposed to your wholy artificial distinction between for & foreach, which are for all intents and purposes identically functional synonyms, excepting for some obscure implementation distinction that nobody can think of a use for.

Which is more useful? Merlyn's "for(list) is a misspelling of foreach" cos he say's it's so; or ikegami's using a counting loop can save me gob loads of memory!


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^6: For vs. Foreach
by merlyn (Sage) on Feb 15, 2009 at 04:40 UTC
    I can't believe you're saying stuff like "As opposed to your wholy artificial distinction between for & foreach, which are for all intents and purposes identically functional synonyms, excepting for some obscure implementation distinction that nobody can think of a use for.".

    /me sighs.

    Do you not see that fundamentally, the C-style for loop (three parts in prefix, no automatic localization) uses a different pattern of behavior from a csh-style foreach loop (always one localized variable, always stepping forward one item at a time through a list or array)? That's not just an "obscure implementation distinction". And that I choose to keep it simple for people by omitting "C-style" and "csh-style", using the shorthand "for" vs "foreach". It's not just that the keywords are interchangeable... they are fundamentally different types of loops, much as a while loop is a third category. Or are you going to say that "while is just a for loop with some obscure implementation distinction" as well? Madness.

      Of course the C-style and csh-style forms are fundamentally different. No one was arguing against that.

      The "obscure implementation distinction" to which I referred is one that javafan pointed out in Re^2: For vs. Foreach. Beyond that obscure, and as far as I can tell useless, implementation distinction, the keywords for & foreach are interchangeable, and can both be used to construct either form--C-style or csh-style--of loop.

      Your unilateral decision to use the keyword for to refer exclusively to the C-style form, and foreach to refer to the csh-style form, (and to consider the alternate uses as "misspellings"), is arbitrary and capricious, and does not succeed in "keep[ing] it simple for people using the shorthand "for" vs "foreach".".

      Given the prevalence of available code that uses either keyword for either form of loop, it will just further confuse things. Far better to just say it like it is, for & foreach are synonyms and either can be used to construct either of two forms of loop.

      There is a good argument that the keywords should have been dedicated one to each of the two forms, along the lines which you favour, and were we at the point where that decision was being made, I would have backed you in that argument. But that time has long gone.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.