Hi,

I'm trying to write some simple code to sort some lines from a log file. Each line in the log file lists a message reference, and which part (out of a total count) that particular line is.

E.g.
Message reference 1, part 1 of 2
Message reference 1, part 2 of 2

I need to sort all the parts to a particular reference in ascending order and then return the log line number.

I've started work on trying to do this with perl hashes, but being a perl newbie, I'm struggling to get the return references. If anybody can help to point me in the right direction that would be much appreciated.

Using my sample code a data (below), I can currently print out
Ref: 1, parts: 1, 2, 3
Ref: 2, parts: 2, 3, 4

Ideally I want it to show
Ref 1, lines 5, 4, 6
Ref 2, lines 17, 18, 16

but I'm stuck trying to figure out how to pull out the key from %Data, given the part number associated with a reference.

Hopefully that makes some sort of sense.

#!/usr/bin/perl use strict; use warnings; my %Data = ( 04 => 'BQADAQMB (part 2 of 3 of message reference 1)', 05 => 'BQADAQMC (part 1 of 3 of message reference 1)', 06 => 'BQADAQMD (part 3 of 3 of message reference 1)', 16 => 'BggEAAIDAQ== (part 4 of 3 of message reference 2)', 17 => 'BggEAAIDAg== (part 2 of 3 of message reference 2)', 18 => 'BggEAAIDAw== (part 3 of 3 of message reference 2)', ); my %table; foreach my $key (sort keys %Data) { my $lValue = $Data{$key}; my @array = split ' ', $lValue; my $lPartStart = $array[2]; my $lPartEnd = $array[4]; my $lReference = $array[8]; chop($lReference); $table{$lReference} = [] unless exists $table{$lReference}; push @{$table{$lReference}}, $lPartStart; } foreach my $lReference (sort keys %table) { print "Ref: $lReference, parts: "; my @parts = @{$table{$lReference}}; print join ', ', sort @parts; print "\n"; }

In reply to Help needed sorting data with multiple associated hashes by willk1980

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.