Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Breaking out of a loop

by kennethk (Monsignor)
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-10-30 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls