On 5/4/10 Tue  May 4, 2010  4:52 PM, "Paul Fontenot" <wpfonte...@cox.net>
scribbled:

> Hi,
> 
> I'm stuck on using an array to determine the out come of a foreach loop.
> The script is below.
> ------------------------------------------------------------------------------
> ------------------------------
> #!/usr/bin/perl
> #
> @conditions = ("NET", "eth");
> $hostname   = (`/bin/hostname`);
> $logfile    = "/var/log/messages";
> $output     = "/root/bin/output.txt";
> 
> open(OUTPUT, ">>", $output) or die $!;
> open(LOGFILE, "<", $logfile) or die $!;
> 
> foreach $condition (@conditions) {
>          print "\n";
>          print "$condition detected on $hostname";
>          print 
> "=========================================================================\n";
> 
>          foreach $line (<LOGFILE>) {
>                  if ($line =~ /$condition/) {
>                          print $line;
>                  }
>          }

       seek(LOGFILE,0,SEEK_SET) or die("Can't seek $logfile: $!");
> }
> 
> close(OUTPUT);

You need to reset LOGFILE to the beginning for subsequent iterations over
the @conditions array. As written, the nested foreach will never be executed
except for the first condition. Add the indicated seek call.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to