Monks,

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

@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) ] );
Is there a better way to do this without regenerating the array every time a message is added?

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.


In reply to Converting a growing hash into an array of arrays by madbombX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.