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

How do I parse and evaluate a file line-by-line?

by kendroid (Beadle)
on Mar 03, 2001 at 08:12 UTC ( [id://61968]=perlquestion: print w/replies, xml ) Need Help??

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

My question is similar to this one, but I'm pretty much a newbie so I have a question in addition: I understand how to throw a file line-by-line into an array, but how could I search the array for "Down" in one element and "Up" in a following (not necessarily the next) element? I'm trying to generate stats from a router log that looks something like this:
a_time_down Down a_time_up Up a_time_down Down a_time_down Down a_time_up Up
I have to come up with a aggregate downtime from a log like this. I found Time::ParseDate (what a blessing!), so once I know which Down element cooresponds with the next Up, tallying up the downtime shouldn't be a problem. Thanks so much for your help; I'm pretty new to Perl so please be kind. :)

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I parse and evaluate a file line-by-line?
by dfog (Scribe) on Mar 03, 2001 at 11:11 UTC
    Assuming that you read in the file into the array using
    @lines = <FILE>;
    and that the array is already in chronological order, you could loop through matching downs with ups in the following way:
    my $count = -1; # set to -1 for loops sake and autoincrementing my $placeHolder = 0; # remembers place in array for the first down my $totalDownTime = 0; while ( $count++ <= $#lines ) { if ( $lines[$count] =~ /Down/ ) # finds first down; { $placeHolder = $count; # the following while increments until it finds the next up, # or the end of the array while ( ( $count++ <= $#lines ) && ( $lines[$count] =~ /Down/ +) ) { # do nothing in the while loop } $totalDownTime += sum( $lines[$placeHolder], $lines[$count] ); # sum will be a function defined by you that takes the lines w +ith # down and its corresponding up, parses them, # and returns the time difference } }
      and that the array is already in chronological order, you could loop through matching downs with ups in the following way:

      The code I snipped below the above quote seems to be aimed at some specific parsing of some specific kind of file, and thus hardly answers the question contained in its own Subject, namely "How do I parse and evaluate a file line-by-line?" Or am I missing something? If not, then isn't this OT?

      When you read data from a file into an array it is already in order. If you want to read the last element of the array then simply use array-1 and then keep incrementing to go from the bottom up. Hope this helps. Shashidhar Iddamsetty
        When you read data from a file into an array it is already in order.

        Yep, the original node said so, too.

        If you want to read the last element of the array then simply use array-1 and then keep incrementing to go from the bottom up.

        Wasn't this basically your other reply too? At least there it made some (i.e. "a very limited amount of") sense. Here, it's completely OT wrt what you're replying to. Which in turn seems to be OT wrt its own subject...

        Hope this helps.

        I don't think so. Sorry.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 07:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found