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

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 ( #585927=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re^2: Efficient file handling (was Re^3: trouble parsing log file...)
Select or Download Code
Re^3: Efficient file handling (was Re^3: trouble parsing log file...)
by Anonymous Monk on Jul 05, 2009 at 03:09 UTC
    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://585927]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (18)
As of 2014-07-14 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (260 votes), past polls