Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > perl hashes

Reply
Thread Tools

perl hashes

 
 
Bart Grieten
Guest
Posts: n/a
 
      04-03-2004
Hi everyone, I'm not an expert Perl programmer and I'm trying to sort a
hash.
I know how to sort it by it's keys, but this time it should be sorted by its
values.

So if I have

%hash = (
Apples => 1,
apples => 4,
artichokes => 3,
Beets => 9,
);

then it should be sorted and printed in this way:

Beets 9
apples 4
artichokes 3
Apples 1

Can anyone help me with this problem?

Thanks



 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-03-2004
[ Do not post the same question in multiple newsgroups!! ]

Bart Grieten wrote:
> I'm trying to sort a hash.
> I know how to sort it by it's keys, but this time it should be
> sorted by its values.


And if you don't know how to do that, it's good idea to look it up,
don't you think?

perldoc -f sort

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
hubert depesz lubaczewski
Guest
Posts: n/a
 
      04-03-2004
Bart Grieten wyrze╝bi│(a):
> %hash = (
> Apples => 1,
> apples => 4,
> artichokes => 3,
> Beets => 9,
>);
> then it should be sorted and printed in this way:
> Beets 9
> apples 4
> artichokes 3
> Apples 1


using variable number of spaces make the whole thing very complicated.
yet. if the number of spaces should be the same, you simply:

for my $sKey (sort { $hash{$b} <=> $hash{$a} } keys %hash) {
printf("%s : %u\n", $sKey, $hash{$sKey});
}

depesz

--
*-----------------------------------------------------------------*
czaderskie dywaniki do │azienki lub przed │ˇ┐ko:
http://www.allegro.pl/show_user_auctions.php?uid=118670
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
On Hashes - How the hashes printing works? Neela megha shyam Chivukula Ruby 4 05-28-2009 10:56 AM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments