madbombX has asked for the wisdom of the Perl Monks concerning the following question:
I am attempting to write a script that takes all the data in a log file and parses out for a value. Ie, it takes the amavid-new logfile and pulls out the SPAM hit points per message and continually tails the file (using File::Tail) and a forked (daemonized process) and adds values to the hash as new messages come in. I am also trying to graph these values in a bar graph in order to see trends. I have settled on using GD::Graph::bars as opposed to RRDs.
My question is that incrementing a value in a 'Key => Value' pair is easy, but not in an array that is required to look as such (at least not to me):
Is there a better way to do this without regenerating the array every time a message is added?@data = ( ["1.6","2.2","3.4","3.6","5.4","6.2","7.1", "8.1", "9.0"], [ 1, 2, 5, 6, 3, 15, 4, 3, 4], [ sort { $a <=> $b } (1, 2, 5, 6, 3, 15, 4, 3, 4) ] );
Or, would it potentially be better to do this all using RRDs (even though the aspect of time that RRD takes full advantage of is irrelevant). I just want to keep the # of messages per point total (I know the second portion of the question is barely Perl related, but I many out there are more experienced than I.
Thanks. Eric
UPDATE: Here is the shortened version of the completed (working) code.
$log = File::Tail->new( name => $MAILLOG, tail => -1); while (defined(my $line=$log->read)) { IncrData(Get_Hits($line)); if (($msgs{Total} % 200) == 1) { Create_Graph(); } } sub IncrData ($) { my $values = shift; if (exists $hits{$values} ) { ${$hits{$values}}++; } else { my $idx = 0; my $endIdx = scalar(@{$data[0]}); while ($idx < $endIdx && $data[0][$idx] < $values) { $idx++; } splice(@{$data[0]},$idx,0,$values); splice(@{$data[1]},$idx,0,1); $hits{$values} = \$data[1][$idx]; } }
Since the file is being tailed, I have the graph being recreated every 200 incoming messages. Just before the graph gets recreated, I run the sort code: @{$data[2]} = sort { $a <=> $b } @{$data[1]};
Again, thanks to all for all the help.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Converting a growing hash into an array of arrays
by jmcada (Acolyte) on Jul 14, 2006 at 16:52 UTC | |
by madbombX (Hermit) on Jul 14, 2006 at 17:17 UTC | |
by jmcada (Acolyte) on Jul 14, 2006 at 18:18 UTC | |
by holli (Abbot) on Jul 14, 2006 at 19:42 UTC | |
by madbombX (Hermit) on Jul 14, 2006 at 19:24 UTC | |
by shonorio (Hermit) on Jul 14, 2006 at 21:06 UTC | |
|
Re: Converting a growing hash into an array of arrays
by rodion (Chaplain) on Jul 14, 2006 at 21:24 UTC | |
|
Re: Converting a growing hash into an array of arrays
by kwaping (Priest) on Jul 14, 2006 at 21:22 UTC |