develooper Front page | perl.beginners | Postings from February 2002

sorting with complicated data structure

Thread Next
From:
Charles Lu
Date:
February 24, 2002 09:28
Subject:
sorting with complicated data structure
Message ID:
F1483ednHM3SOlbpH7100023470@hotmail.com
Hi all

   I am attempting to sort the following data.  But I cannot get it to work. 
  Frankly i am not even sure I am allowed to do this in perl.  Nonetheless I 
hope anyone can give me some suggestions.
Here is the data structure

$query->{$reference}->[0] = ['a','b', 9, 12];
$query->{$reference}->[1] = ['c','d', 4, 15];
$query->{$reference}->[2] = ['e','f', 8, 30];

so for example to print out the entire 1st array i would type

print "@{ $query->{$reference}->[0] }";

I want to sort the above 3 arrays by their 3rd element in ascending order.  
So the final correct order would be

$query->{$reference}->[2] = ['a','b', 9, 12];
$query->{$reference}->[0] = ['c','d', 4, 15];
$query->{$reference}->[1] = ['e','f', 8, 30];

#############################################
HERE IS MY CODE (IT DOESN'T WORK)
sub by_third_item {

    return $query->{$reference}->[$a][2] <=> $query->{$reference}->[$b][2];

}

@sortedlist = sort by_third_item @{ $query->{$reference} };
#############################################

When run, the program "hangs" and i had to kill it.  Can anyone suggest what 
I am doing wrong?  Thank you.


charles


_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About