Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Efficient file handling (was Re^3: trouble parsing log file...)

by perl_geoff (Acolyte)
on Nov 24, 2006 at 18:01 UTC ( #585923=note: print w/replies, xml ) Need Help??

in reply to Efficient file handling (was Re^3: trouble parsing log file...)
in thread trouble parsing log file...

Hi, I tried to do this and couldn't get it to work correctly, can you show me what I'm doing wrong?
use strict; use warnings; my $logfile="log.txt"; my $error="DOWN"; my $warn="PROBLEM"; my $redbutton="\<img src\=\'default_files/perlredblink\.gif'>"; my $greenbutton="\<img src\=\'default_files/perlgreenblink\.gif'>"; my $yellowbutton="\<img src\=\'default_files/perlyellowblink\.gif'>"; open LOG, $logfile or die "Cannot open $logfile for read :$!"; my $button = $greenbutton; my @logfile=<LOG>; # throw logfile into an array while (<LOG>) { if ($_ =~ /$error/i) { $button = $redbutton; print "<!--Content-type: text/html-->\n\n"; print "$button"; last; } elsif ($_ =~ /$warn/i) { $button = $yellowbutton; print "<!--Content-type: text/html-->\n\n"; print "$button"; last; } else { print "<!--Content-type: text/html-->\n\n"; print "$button"; last; } } close LOG;
  • Comment on Re: Efficient file handling (was Re^3: trouble parsing log file...)
  • Download Code

Replies are listed 'Best First'.
Re^2: Efficient file handling (was Re^3: trouble parsing log file...)
by Not_a_Number (Prior) on Nov 24, 2006 at 18:47 UTC

    First and foremost, your line:

    my @logfile=<LOG>;    # throw logfile into an array

    does exactly what the comment says: it reads the whole logfile into an array (which you subsequently never use). That means that the test in the following line:

    while ( <LOG> ) {

    can never be true: the filehandle has already reached the end of the file by the time this line is reached. Just get rid of the first of these two lines.

    However, once this problem is fixed, it becomes clear that your program logic is flawed. The program will only ever read just one line from the logfile, because all the branches of the if/elsif/else structure end with last. So if the first line of the logfile contains, say, just "foo", it will print a green button and stop executing, even if the second (or third...) line is "SERVER DOWN".

    Lastly, as your regexes stand, $error will match eg "Downing Street", which is probably not what you want.

    I suggest that you use something like the following (simplified for the purposes of this posting to read from __DATA__ rather from a filehandle, and to output a simple string):

    use strict; use warnings; my $error = 'DOWN'; my $warn = 'PROBLEM'; my $redbutton = 'RED BUTTON'; my $greenbutton = 'GREEN BUTTON'; my $yellowbutton = 'YELLOW BUTTON'; my $button = $greenbutton; while ( <DATA> ) { if ( /\b$error\b/i ) { $button = $redbutton; last; } elsif ( /\b$warn\b/i ) { $button = $yellowbutton; } } print $button; __DATA__ foo Watership Down bar
      OK, in light if this discussion, I have a similar problem.
      I need help, I have a script that i am trying to do the following:
      file1= list of unique ID numbers
      file2= list of unique html code with the unique ID numbers within the code per line(about 16k-bytes each seperated by carage.
      Both are standard text files.
      #read each line in test1.txt into data_file array
      open(DATA, $data_file) || die("Could not open file!");

      #read each line in code.txt into a names_file array
      open(NAMES, $names_file) || die("Could not open file!");

      #create loop that reads each ID in code.txt (NAMES array), searches for each in array elements for #test1.txt (DATA array), redirects a new (NAMES).html for each element
      foreach ( $NAMES )
      ($NAMES=$DATA<0> > +("$NAMES<0>.html"));

      close NAMES;
      close DATA;

      I am new to perl but this is absolutely riddled with errors and I have written this according to examples of similar scripts.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://585923]
and !@monks...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2018-07-18 05:23 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (383 votes). Check out past polls.