> Shouldn't that be "values %hash" for sorting the values (see below), > also be aware that a hash has no order like an array so you just have to > do what you want while in the foreach loop (or stick them into an > array). > > my %hash; > foreach(sort {$hash{$a} <=> $hash{$b} } values %hash) { > .... > }
Not unless you are planning on using the values by themselves... For example: my %hash=(thumb='122',tom=>'21',muffit=>'48',miss=>'31'); foreach(sort { $hash{$a} <=> $hash{$b} } keys %hash) { print " $_ ($hash{$_})" if(/^m/); } result: miss (31) muffit (48) foreach(sort { $hash{$a} <=> $hash{$b} } values %hash) { print "$_\n"; } result: 21 31 48 122 The later is the same as: my @values=values %hash; for(sort {$a <=> $b} @values) { print "$_\n"; } If the later is all you are after, then values is what you want, but if you still want to key off of the hash itself, then you want to go with keys... I have never had a use for the later, but that is not really saying anything either... Shawn > > > >>> "Shawn" <[EMAIL PROTECTED]> 06/24/02 07:49am >>> > Hello Pat, > You can do it like so: > > my %hash; > foreach(sort {$hash{$a} <=> $hash{$b} } keys %hash) { > .... > } > > Shawn > > ----- Original Message ----- > From: "Postman Pat" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Monday, June 24, 2002 1:46 AM > Subject: sorting a hash by value. > > > > Greetings, > > I would like to sort a hash by value. The hash values are numbers. I > would > > like to sort this by desceding order. How would I do this? I have > searched > > and found nothing on it. I have found lots on sorting by key > though... > > Please help dudes! > > > > Ciao > > > > LK > > > > -- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > > ITM Business Solutions > Unit 4 > Nine Trees Trading Estate > Morthen Road > Rotherham > S66 9JG > > Reception > Tel: 01709 703288 > Fax: 01709 701549 > > Help Desk > Tel:01709 530424 > Fax: 01709 702159 > > CONFIDENTIALITY NOTICE: This message is intended only for the use of > the individual or entity to which it is addressed, and may contain > information that is privileged, confidential and exempt from disclosure > under applicable law. > > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]