rodbass63 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need help breaking out of a loop and going to a else statement if I can't find a string in a file. The 'yum_sorted.log' file is a list of new pkgs. I want to see if each pkg in the yum_sorted.log file is in the git_sorted file; if it's a match, move on to the next pkg name. If after slurping the whole git_sorted.log file that a pkg is not there, I want it to go to a else statement and print "Pkg is not there in the git_sorted.log file!"
#!/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>; 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"; } }
### # Thanks in advance!

Replies are listed 'Best First'.
Re: Breaking out of a loop
by poj (Abbot) on May 12, 2013 at 14:19 UTC
    By using a hash you can avoid scanning the git file multiple times. For example ;
    #!/usr/bin/perl use warnings; use strict; my %git=(); my $git_file = "/tmp/git_sorted.log"; open(GIT,'<',$git_file) or die "Really bad $git_file : $!"; while (<GIT>){ chomp; $git{$_}=1; } close GIT; my $yum_file = "/tmp/yum_sorted.log"; open(YUM,'<',$yum_file) or die "Really, really bad $yum_file : $!"; while (<YUM>){ chomp; if (exists $git{$_}){ # exists } else { # new print "$_ package not in $git_file\n"; } } close YUM;
    poj
      The logic in your code is vastly superior to the original. However, the comparison you make does not do the same thing that the original.

      The original attempts a regular expression match of the line read against each element of the git array, while yours expects the entire line to match one element exactly.

      For this case, an array structure for "git" with a "grep" may work better. Of course, it depends on what the data looks like.

                   "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
              -- Dr. Cox, Scrubs

Re: Breaking out of a loop
by kennethk (Abbot) on May 12, 2013 at 13:55 UTC
    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.