Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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.

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 having an uproarious good time at the Monastery: (10)
As of 2016-10-24 19:29 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (309 votes). Check out past polls.