A database solution for you to consider ;
#!perl use strict; use warnings; use DBI; # set up my %entry=(); while (<DATA>){ my ($id,$score,$locn) = split '\s+',$_; push @{$entry{$id}{Score}} ,$score; push @{$entry{$id}{Location}},$locn; } # create db my $dbfile = 'scores.sqlite'; unlink($dbfile); my $dbh = DBI->connect('dbi:SQLite:dbname='.$dbfile , undef , undef , {RaiseError =>1, AutoCommit =>1}) or die $DBI::errstr; $dbh->do('CREATE TABLE SCORES (ID,SCORE integer,LOCATION)'); $dbh->do('CREATE TABLE LEAGUE (ID, HI_SCORE integer)'); # load data my $sth = $dbh->prepare('INSERT INTO SCORES VALUES (?,?,?)'); for my $id (keys %entry){ my $rec = $entry{$id}; my @score = @{$rec->{'Score'}}; my @locn = @{$rec->{'Location'}}; for my $i (0..$#score){ $sth->execute($id,$score[$i],$locn[$i]); } } # aggregate $dbh->do('INSERT INTO LEAGUE (ID, HI_SCORE) SELECT ID, MAX(SCORE) FROM SCORES GROUP BY ID'); # report my $sql = 'SELECT S.ID, SCORE, LOCATION FROM SCORES AS S LEFT JOIN LEAGUE AS L ON S.ID = L.ID ORDER BY HI_SCORE DESC, SCORE DESC'; my $ar = $dbh->selectall_arrayref($sql); for (@$ar){ printf "%-5s %5d %10s\n",@$_,"\n"; } $dbh->disconnect; __DATA__ TRA 15 7-15 TRA 23 4-19 TRA 2 78-120 BLA 5 1-10 BLA 10 2-10
poj

In reply to Re: Advance Sorting by poj
in thread Advance Sorting by Anonymous Monk

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.