in reply to Any easy way to do this?
Any a priori assumptions that might provide sensible aid to reduce the user's "cognitive load" would be worth building into the script -- e.g. maybe threshold values should always be evenly spaced over an appropriate range, and users would just say how many thresholds (histogram bins) they want on a given run.
Regarding the code you posted, I'd offer a few "stylistic" points:
That will make it easy to offer useful default values for things like number of bins, start-time and end-time. There could even be a default value for the name of the log file to read.use Getopt::Long; # or Getopt::Std, which might be easier to grok.
Perl gives a warning about line 59 -- it's harmless, but worth fixing.
When there's an "if" block that always ends with "exit 1" (which should just be "die"), there's no need for an "else" block after that (you can eliminate a layer of embedding). Likewise, you don't need an "else" block that contains just a next statement, given that there's nothing after that block in the enclosing loop.
Assuming you have an array of threshold values, you just need to make sure the array values are sorted, and loop over them to work out which bin a given value should be counted in -- here's a simple example that leaves aside all your other issues about selecting/excluding log entries:
(UPDATED to give appropriate scope to $i -- thanks to wfsp for pointing that out.)
Geez! As GrandFather points out below, I really didn't get that right. Even after wfsp had told me it wouldn't work, I still had it wrong. What I should have suggested was something like this (thanks, GrandFather):
my @thresh = ( 1000, 4000, 7000, 10000 ); my @bins; while (<LOG>) { my $val = ( split )[10]; next unless ( $val =~ /^\d+$/ ); my $i = 0; while ( $i < @thresh and $val > $thresh[$i] ) { $i++; } $bins[$i]++; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Any easy way to do this?
by jb60606 (Acolyte) on Sep 15, 2011 at 23:23 UTC | |
by graff (Chancellor) on Sep 16, 2011 at 00:17 UTC | |
by GrandFather (Saint) on Sep 16, 2011 at 07:57 UTC |