Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: array looping with foreach

by mpeg4codec (Pilgrim)
on Jan 18, 2008 at 22:54 UTC ( #663166=note: print w/ replies, xml ) Need Help??


in reply to array looping with foreach

Why not use the traditional C-style version of for as amarquis hinted?

for (my $i = 0; $i < @array; ++$i) { # do something with $array[$i] # do something with $i }


Comment on Re: array looping with foreach
Download Code
Re^2: array looping with foreach
by rowdog (Curate) on Jan 19, 2008 at 00:53 UTC
    It's fun to see all the variations (map/module/each/iter) but in my mind, the C-Style loop wins (mpeg4codec++). I've always used this construction, maybe it's just because I learned C long before Perl, but it's a clear and well known construction that doesn't create extra arrays, use Module.pm, or have the (probably trivial) function call overhead of iterators. I wonder if we don't occasionally try to be too Perlish just because this is NOT C.
      I wonder if we don't occasionally try to be too Perlish just because this is NOT C.
      I thought that was the point! We try to be Perlish because this isn't language X. Perhaps our definitions of "Perlish" differ.

      Familiarity is not a good argument as not everyone comes from C. I suspect people who came from say a lisp or python background would not immediately recognize a C style for loop.

      I will admit it is likely the most efficient, but, for me, that time is lost in correcting all my stupid off by one errors.
        I thought that was the point! We try to be Perlish because this isn't language X. Perhaps our definitions of "Perlish" differ.

        There really is no truly Perlish idiom for this situation. The idiom in OP's second example has some benefits: it's simple, doesn't require any extra modules, is hard to screw up, and lets you safely insert or delete from your array.

        But -- playing language designer for a moment -- could it be better?

        for my $i (0..$#array) { my $el = $array[$i]; ... };

        Hmm, too much repetition. Perhaps:

        over (@array) as ($el; $i) { ... };

        The array element and the index are implicitly localized to the loop, and the index is optional. If you want to iterate n-at-a-time you can do:

        over (@array) as ($el_a, $el_b; $i, $j) { ... };
Re^2: array looping with foreach
by GrandFather (Cardinal) on Jan 19, 2008 at 07:10 UTC

    There is never a need in Perl to use a C style for loop to iterate over a range like that. Instead use the safer and clearer:

    for my $index (0 .. $#array) { ... }

    There are situations where a C style for loop is appropriate in Perl, but they are in fact fairly rare. About the only case that readily comes to mind is where a complicated termination test is required, and even then it is often clearer to use last with a statement modifier in the body of the loop. If multiple statements need to be executed at the end of each iteration you should use a continue block rather than pile all the statements into the iteration part of a C for loop header.


    Perl is environmentally friendly - it saves trees

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-09-16 05:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (156 votes), past polls