Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Can I count lines in a substring?

by suaveant (Parson)
on Jul 31, 2012 at 20:29 UTC ( #984659=note: print w/ replies, xml ) Need Help??


in reply to Can I count lines in a substring?

Certainly you can, and there are any number of ways to do it. One of the simplest might be to read in the whole file and split it on /^Traceroute:/xms and work with the chunks, but if the file is large (or could be large in the future) that may not be the best method. Going with what you have now I'd say do something like...

while (my $line = <$in>) { if($line =~ /^traceroute to (\S+)/) { $current_traceroute = $1; $count = 0; #reset the count; print "Trace for $current_traceroute: "; } elsif($line =~ /^\s*\d+\s+/) { $count++; } elsif($line =~ /^Query:/) { print "$count\n"; } }
Basically you want to catch the beginning (and end) of each block and reset things to start a new count and/or pull any other relevant data. If Query: isn't reliably there then you might have to rely on "Traceroute:" or "traceroute to" to signal the end of the previous call, but then you'd have to handle the last route by triggering on eof since there would be no final "Traceroute:". Make sense?

                - Ant
                - Some of my best work - (1 2 3)


Comment on Re: Can I count lines in a substring?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://984659]
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-07-10 07:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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








    Results (202 votes), past polls