Problem background is that we have a file that contains logs of data for error codes, and my boss now wants this to be tallied up into something that is useable for analysis. There are 3 basic pieces of information that need to be tallied :

Country
Error code
Message Type

The requirement is to be able to produce a report at anytime from this file to show the number of errors per Message type per error code per country, as well as the number of errors per Message type per country per error code.

My immediate thought was to do a 3 dimensional hash as it would be indexed and therefore should be easier to manipulate and handle. However, I am really struggling to determine how to define the hash below the 2nd level.

For example for the first scenario:
%hash=("$country"=>{ "$errorcode=>{ $messagetype=>{ "count"=>"value"}}})
Then to check for existence of a key it would be :
if (defined $hash{$country}{$errorcode}{$message}{$count}) { $hash{$country}{$errorcode}{$message}{$count}+=1; } else { $hash{$country}{$errorcode}{$message}{$count}=1; }
Then we get onto the problem of printing the darn thing.
for $varctry(keys %hash) { for $varerror(@$hash{$varctry}) { for $varmsgtype(@$hash{$varctry}{$varerror}) { print "Country $varctry with error $varerror for msg $varmsgtype h +as $hash{$varctry}{$varerror}{$varmsgtype}{count} errors\n"; } } }
I have tried various combinations, but a lot of the time I am getting warnings back saying that I do not have even amounts of values in my hash, meaning that I don’t have a straight key to value relationship, so I must be missing something obvious here! Any tips you have for me would be greatly appreciated.
Thank you in advance.

In reply to 3 dimensional hashes! by mark_briscombe

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.