Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1033198]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2018-06-21 15:07 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.