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
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
> -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
>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
>> "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
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
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
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
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
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.
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
--- "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.
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
--- 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
--- 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
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
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
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
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
19 matches
Mail list logo