Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Breaking out of a loop

by kennethk (Abbot)
on May 12, 2013 at 13:55 UTC ( #1033198=note: print w/ replies, xml ) Need Help??

in reply to Breaking out of a loop

You probably want to use last, as described in Loop Control in perlsyn. That will let you exit the loop at will. You can then track whether you were successful by having a variable with scope outside your loop.

#!/usr/bin/perl use warnings; use strict; my $git_dir = "/tmp/yum_sorted.log"; open(DIR,$git_dir) or die "Really bad!$!"; my @yum = <DIR>; my $hit; # <--- Inserted lines foreach my $line ( @yum ) { open(IN_GIT, "/tmp/git_sorted.log") or die "Really, re +ally bad $!"; my @in_git = <IN_GIT>; foreach my $x ( @in_git ) { if ( $line =~ $x ) { print "We found a match: $line\n"; $hit = $line; # <--- Inserted lines last; # <--- Inserted lines } } if (defined $hit) { # <--- And final inserted conditional # Do something } else { # Do something else }

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Comment on Re: Breaking out of a loop
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2015-11-29 13: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 (751 votes), past polls