I've been going over the perldocs and trying to figure this out for a few weeks now.. I'm rusty, but am trying to make use of map() to convert an array of lines from a log file into a hash of array of arrays keyed by one of the values in the logfile line.

An example format for a logline: 2010-08-10 13:59:54 GGS INFO 260 Something Something Manager for Monitor, mgr.prm: Lag for REPLICATION REP is 00:00:00 (checkpoint updated 00:00:02 ago).

The $key, in this line, would be "260" (without quotes).

The value would be the line itself, but as an array.

There could be many such "values" per $key, so the $key is not unique in this logfile. I thought a hash of arrays would fit the bill because,for each $key, there' be several lines.

An excerpt from my current attempts: my %sorted_msgs = map { $_[5], $_[0] } sort { Date_Cmp($a->[1], $b->[1]) } map { my @logline = split(/\s+/, $_, 5); my $timestring = '\'' . $logline[0] . ' ' . $logline[1] +. '\''; [ $_, ParseDate($timestring), $logline[0], $logline[1], +$logline[2], $logline[3], $logline[4] ]; } @matched_lines;

In the split, after #5, I capture the rest of the logline.

The ParseDate is from Date::Manip, and works flawlessly in other routines, so i hope that doesn't distract from the question.

The first, inner map, is where I transform the array into anther, temporary array of values that I munged and/or want to be able to access. Then I do a sort on the timestamp, and, lastly, I (am trying) to convert values in the transformed array into a hash of arrays.

Dumper shows: DUMPER: $VAR1 = 'HASH(0x82442a0) ';


In reply to Using map to convert an array into a hash of array of arrays by rjoost

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.