Date: Tue, 26 Jun 2001 11:19:30 -0400 From: tadmc@augustmail.com (Tad McClellan) Subject: Re: how to sort array of similar hashes by one of the hash keys? Message-Id: Irfan Baig wrote: >I've split data from a flatfile into hashes in an array, @game, all of >which have keys 'name', 'score' and 'date'. > >@game = (); >for ($i=0; $i<=$#scores; $i++){ > ($game[$i]{'name'},$game[$i]{'score'},$game[$i]{'date'}) = split(/,/, $data[$i]); >} So each element in @game is a reference to a hash then. >I'm now trying to sort the array by comparing the values of the 'score' elements >in each array's hash. Is this possible? I've tried some combinations such >as: > >@sorted_game = sort{ $game[$a]{'score'} <=> $game[$b]{'score'} } @game; > >but no luck. Can someone help me out? You are sorting a list of _references_ to the hashes, so $a and $b will be hash refs, but you do not deref them. $a/$b are not small integers suitable for array indexing: @sorted_game = sort{ $a->{'score'} <=> $b->{'score'} } @game; -- Tad McClellan SGML consulting tadmc@augustmail.com Perl programming Fort Worth, Texas