Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^2: For vs. Foreach

by Eimi Metamorphoumai (Deacon)
on Feb 13, 2009 at 18:25 UTC ( #743658=note: print w/replies, xml ) Need Help??

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

I'm curious why you consider the "counting loop" to be a fundamentally different kind of loop, instead of listing it among the optimised iterative loops. Is there any difference as far as observable behavior, or is it just that it's implemented differently, or is there really no difference but that's how you conceive of it?

Replies are listed 'Best First'.
Re^3: For vs. Foreach
by ikegami (Pope) on Feb 13, 2009 at 19:04 UTC

    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).

      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.


      ack Albuquerque, NM
      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 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?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2020-10-20 08:50 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (209 votes). Check out past polls.