Contributed by kendroid
on Mar 03, 2001 at 08:12 UTC
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:
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
and that the array is already in chronological order, you could loop through matching downs with ups in the following way:
@lines = <FILE>;
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
# 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:
Outside of code tags, you may need to use entities for some characters:
- 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
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||