Re: sorting a hash by value.

2002-06-24 Thread Jeff 'japhy' Pinyan
On Jun 24, Postman Pat said: >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... Where did you search? A single check to

Re: sorting a hash by value.

2002-06-24 Thread Nigel Peck
For some stupid reason I was imagining that foreach(sort { $hash{$a} <=> $hash{$b} } keys %hash) { was going to sort by the keys and not the values, overlooking the comparison being based on the value, thanks for pointing it out (now I understand :-) >>> "Shawn" <[EMAIL PROTECTED]> 06/24/02 10

RE: sorting a hash by value.

2002-06-24 Thread Bob Showalter
> -Original Message- > From: Postman Pat [mailto:[EMAIL PROTECTED]] > Sent: Monday, June 24, 2002 2:46 AM > To: [EMAIL PROTECTED] > Subject: sorting a hash by value. > > > Greetings, > I would like to sort a hash by value. The hash values are > numbers. I

Re: sorting a hash by value.

2002-06-24 Thread Shawn
>foreach(sort { $hash{$a} <=> $hash{$b} } values %hash) { > print "$_\n"; >} Err... This will not work... You will be trying to get a hash value based on the value... It's early my %hash=(thumb='122',tom=>'21',muffit=>'48',miss=>'31'); foreach(sort { $hash{$a} <=> $hash{$b} } values %hash

Re: sorting a hash by value.

2002-06-24 Thread 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

Re: sorting a hash by value.

2002-06-24 Thread Nigel Peck
quot; <[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

Re: sorting a hash by value.

2002-06-23 Thread Shawn
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 Subj

sorting a hash by value.

2002-06-23 Thread Postman Pat
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: [EMAI

Re: Sorting a hash by value, and displaying the key

2001-06-07 Thread rob chanter
On Sat, Jun 02, 2001 at 06:13:04PM +1000, iain truskett wrote: > Rob talked about > > foreach $value(sort {$a <=> $b} (values(%hash)) { > print $value; > } > > vs. > > foreach $value(sort byNum (values(%hash)) { > print $value; > } > > sub byNum { > $a <=> $b; > } > > > Am I corr

Re: Sorting a hash by value, and displaying the key

2001-06-02 Thread iain truskett
Rob talked about foreach $value(sort {$a <=> $b} (values(%hash)) { print $value; } vs. foreach $value(sort byNum (values(%hash)) { print $value; } sub byNum { $a <=> $b; } Am I correct in thinking that the anonymous subroutine is vaguely more efficient? -- iain.

Re: Sorting a hash by value, and displaying the key

2001-05-31 Thread Timothy Kimball
Paul Hodges wrote: : Try flipping the hash. : : my %flipped; : @flipped{values %HASH} = keys %HASH; : : That makes the keys of %HASH the values of %flipped, with the matching : values of %HASH being the keys to which they are assigned. : Now just do it the easy way. : : for my $key (sort ke

RE: Sorting a hash by value, and displaying the key

2001-05-31 Thread Paul
--- "Maynard, Garth" <[EMAIL PROTECTED]> wrote: > Caution: if you have any data that is repetitive, you will lose > information during the flipping process. An excellent point. Shouldn't be the case here, since the values were the order of entry, but it needed pointing out.

RE: Sorting a hash by value, and displaying the key

2001-05-31 Thread Maynard, Garth
Caution: if you have any data that is repetitive, you will lose information during the flipping process. Subject: Re: Sorting a hash by value, and displaying the key --- David Michael <[EMAIL PROTECTED]> wrote: > Hello, > I have a hash that needs to be displayed in a cert

Re: Sorting a hash by value, and displaying the key

2001-05-31 Thread Paul
--- Paul <[EMAIL PROTECTED]> wrote: > > --- David Michael <[EMAIL PROTECTED]> wrote: > > Hello, > > I have a hash that needs to be displayed in a certain order. I > > tried > > > > foreach $key (sort (keys %HASH)) { > > print $key; > > } > > > > that sorts alphabeti

Re: Sorting a hash by value, and displaying the key

2001-05-31 Thread Paul
--- David Michael <[EMAIL PROTECTED]> wrote: > Hello, > I have a hash that needs to be displayed in a certain order. I > tried > > foreach $key (sort (keys %HASH)) { > print $key; > } > > that sorts alphabetically. I need it in the order it was > inserted, so I made

Re: Sorting a hash by value, and displaying the key

2001-05-31 Thread rob chanter
On Thu, May 31, 2001 at 02:41:32PM -0500, David Michael wrote: > Hello, > I have a hash that needs to be displayed in a certain order. I tried > > foreach $key (sort (keys %HASH)) { > print $key; > } > > that sorts alphabetically. I need it in the order it was inserte

Re: Sorting a hash by value, and displaying the key

2001-05-31 Thread Timothy Kimball
David Michael wrote: : I have a hash that needs to be displayed in a certain order. I tried : : foreach $key (sort (keys %HASH)) { : print $key; : } : : that sorts alphabetically. I need it in the order it was inserted, so I made the :value a number that increased f

Re: Sorting a hash by value, and displaying the key

2001-05-31 Thread Brett W. McCoy
On Thu, 31 May 2001, David Michael wrote: > Hello, > I have a hash that needs to be displayed in a certain order. I tried > > foreach $key (sort (keys %HASH)) { > print $key; > } > > that sorts alphabetically. I need it in the order it was inserted, > so I made the value a

Sorting a hash by value, and displaying the key

2001-05-31 Thread David Michael
Hello, I have a hash that needs to be displayed in a certain order. I tried foreach $key (sort (keys %HASH)) { print $key; } that sorts alphabetically. I need it in the order it was inserted, so I made the value a number that increased for each key. I need to sort by