Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: For vs. Foreach

by merlyn (Sage)
on Feb 13, 2009 at 21:58 UTC ( #743718=note: print w/replies, xml ) Need Help??


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

If you break out foreach $n (1..10) { } as separate from foreach $n (LIST) { }, then you should also break out foreach $item (@an_array) {} as a special kind of foreach as well, since a list copy is not made.

And then you'd also have to break out a slice too. Or any other array lvalue.

I think your false distinction of "counting loop" as separate is just that. False. There are two kinds of loops: for and foreach. They each have subfeatures. That's it. Not three kinds.

Replies are listed 'Best First'.
Re^5: For vs. Foreach
by BrowserUk (Pope) on Feb 14, 2009 at 04:01 UTC

    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.
      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.
Re^5: For vs. Foreach
by ikegami (Pope) on Feb 14, 2009 at 03:19 UTC

    I think your false distinction of "counting loop" as separate is just that. False.

    It's not my false distinction, like I explained. It's a need of myriads of programmers I support here. If it helps, why should I say "you're all wrong!".

    And then you'd also have to break out a slice too.

    What kind of logic is that? As far as I can tell, that isn't optimised, it holds no special significance to anyone, and there's stylistic advantage for the distinction. There's nothing special about array slices in a loop.

Re^5: For vs. Foreach
by Anonymous Monk on Feb 14, 2009 at 01:27 UTC
    Don't break it, eat the slice :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2020-11-30 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?