Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Foreach Loops

by Roy Johnson (Monsignor)
on Mar 15, 2005 at 22:00 UTC ( #439789=note: print w/ replies, xml ) Need Help??

in reply to Foreach Loops

Updated: MapCar was a bad choice. This one is actually pretty cool:

use Algorithm::Loops 'NestedLoops'; my $iter = NestedLoops([\@arr, do {my $i=0; sub {[$i++]}} ]); while (my ($line, $x) = &$iter) { print "$x: $line\n"; }
Obviously, modifying $line isn't going to affect @arr, though. To do that, you'd have to do
use Algorithm::Loops 'NestedLoops'; my $iter = NestedLoops([[\(@arr)], do {my $i=0; sub {[$i++]}} ]); while (my ($line, $x) = &$iter) { for my $line ($$line) { print "$x: $line\n"; $line = 'X'; } } print "@arr\n";

Caution: Contents may have been coded under pressure.

Comment on Re: Foreach Loops
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2015-11-30 08:30 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (765 votes), past polls