in reply to Advance Sorting

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

Replies are listed 'Best First'.
Re^2: Advance Sorting
by Anonymous Monk on Jun 03, 2014 at 19:42 UTC
    Interesting idea, will give it a try. Thanks for that