I am trying to use hash array to calculate the running time of each cron job. The log file likes following:
== < root 26144 c Tue Nov 2 03:10:02 2010 < oracle 26161 c Tue Nov 2 03:10:25 2010 < oracle 26193 c Tue Nov 2 03:10:30 2010 < sybase 26163 c Tue Nov 2 03:10:32 2010 > oracle 26161 c Tue Nov 2 03:10:33 2010 < sybase 26188 c Tue Nov 2 03:10:38 2010 > sybase 26163 c Tue Nov 2 03:10:58 2010 ==
The leading character "<" means job started, ">" means job finished. The second field is username. Third field is jobID, which might be reused after this one is done. Since all these are daily jobs, I only care field 8 to calculate run time. Here is what I am trying to do:
==start code==
#!/usr/bin/perl use strict; use warnings; use Date::Manip; my @owner= qw/oracle sybase/; open (FILE,"cron.log"); while(<FILE>) { chomp; next if (split /[ ]+/)[1] !~ /oracle|sybase/ ; my ($mode,$owner,$job_id,undef,undef,undef,undef,$timestamp,undef) +=split /[ ]+/; #get same jobID finish timestamp and calculate difference, #then save back ${$owner}{$job_id}=DateCalc(${$owner}{$job_id},$timestamp) if ($mo +de=~ /^>/); } close(FILE); foreach $owner(@owner){ foreach (keys %{$owner}) { print "$owner - JobID:$_ - RunTime:${$o +wner}{$_}\n"; };}
==end code here==
There are three problems with my code:
1. under "strict" mode, it doesn't allow me to use number(jobID in my case) as ARRAY ref.
2. didn't consider same jobID may appeared later
3. It seems "${$owner}{$job_id}" is treated as array reference instead of hash array. When I debug above code, this variable is always null.

Seek a better approach to achieve the target. Thanks in advance!


In reply to hash array by roadtest

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.