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

Re: Exiting the inner loop of a nested loop

by revdiablo (Prior)
on Apr 14, 2006 at 17:45 UTC ( [id://543402]=note: print w/replies, xml ) Need Help??


in reply to Exiting the inner loop of a nested loop

Ah, your update definitely makes it more clear. You're not going to be able to accomplish this by printing and checking at the same time. You'll have to do two passes: one to check for any values that you want to avoid, and another to print out all the values from the ones that are deemed ok. Here's a quick example:

my @num_lists = ( [ 0 .. 4 ], [ 5 .. 9 ], [ 10 .. 14 ], [ 15 .. 19 ], ); OUTER: for my $list (@num_lists) { for my $num (@$list) { if ($num % 10 == 0) { print "Skipping $list\n"; next OUTER; } } for my $num (@$list) { print "From $list: $num\n"; } }

Or you can use grep, which still just loops on the values, but might look a bit cleaner to some:

my @num_lists = ( [ 0 .. 4 ], [ 5 .. 9 ], [ 10 .. 14 ], [ 15 .. 19 ], ); for my $list (@num_lists) { next if grep { $_ % 10 == 0 } @$list; for my $num (@$list) { print "From $list: $num\n"; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found