Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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
Re^2: Efficient file handling (was Re^3: trouble parsing log file...)
by Not_a_Number (Parson) 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 tony.blair@downingstreet.gov.uk 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.
      ===========
      #!/usr/bin/perl
      #read each line in test1.txt into data_file array
      $data_file="test1.txt";
      open(DATA, $data_file) || die("Could not open file!");
      @data_file=<DATA>;

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

      #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 )
      {
      chomp($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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-09-03 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (34 votes), past polls