Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: For vs. Foreach

by ikegami (Pope)
on Feb 13, 2009 at 19:04 UTC ( #743663=note: print w/replies, xml ) Need Help??


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

Good question.

It is truly optimised to count from the first to the last without creating a list. You could lump it as an optimisation since the effect is the same as if ".." was the range operator. (It's not.) I choose not to.

People already have a concept of a counting loop from countless other languages. They expect to find a counting loop and presume Perl provides the C-style for loop that purpose. I'm giving them what they are looking for.

By labeling it as a counting loop, I try to clear the false perception that it should be avoided for efficiency reasons, in order to encourage the use of the for my $i ($x..$y) over the less readable for (my $i=$x; $i<=$y; ++$i).

Replies are listed 'Best First'.
Re^4: For vs. Foreach
by ack (Deacon) on Feb 13, 2009 at 19:16 UTC
    Wow! Thank you!

    I have seen that counting construct (i.e., using the ($x..$y) construct in counting for loops) many times before but in the fog of my mind, I had never "connected" the synapses.

    For me, your advice absolutely really *does* improve the readability and maintainability.

    Your advice got through to my adled brain. So you've got one more convert. Thank you.

    Yoohooo!!!

    ack Albuquerque, NM
Re^4: For vs. Foreach
by merlyn (Sage) on Feb 13, 2009 at 21:58 UTC
    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.

      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.

      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.

      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://743663]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2020-11-24 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?