Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

( #61968=categorized question: print w/ replies, xml ) Need Help??
Contributed by kendroid on Mar 03, 2001 at 08:12 UTC
Q&A  > files


Description:

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. :)

Answer: How do I parse and evaluate a file line-by-line?
contributed by dfog

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 } }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (7)
    As of 2015-07-01 22:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (24 votes), past polls