Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^4: array looping with foreach

by mrpeabody (Friar)
on Jan 19, 2008 at 04:10 UTC ( [id://663193]=note: print w/replies, xml ) Need Help??


in reply to Re^3: array looping with foreach
in thread array looping with foreach

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) { ... };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found