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

Re: sorting with complicated data structure

Thread Previous
From:
Tanton Gibbs
Date:
February 24, 2002 09:57
Subject:
Re: sorting with complicated data structure
Message ID:
004b01c1bd5c$b4538d60$30486e0a@brooklyn
You're close, but are missing what $a and $b are.  They are not indexes, but
are the actual array references.

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


----- Original Message -----
From: "Charles Lu" <charlu76@hotmail.com>
To: <beginners@perl.org>
Sent: Sunday, February 24, 2002 12:27 PM
Subject: sorting with complicated data structure


> 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
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
>


Thread Previous


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