473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

perl hashes

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

Jul 19 '05 #1
2 2344
[ 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

Jul 19 '05 #2
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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
1
by: Matt Breedlove | last post by:
Ok, I dont see why this is so difficult, but it seems like all the solutions I have researched require me to identify each incoming variable which is very undesirable as there are 80+ hashes and...
2
by: scadav | last post by:
I am new to Perl and I am trying for figure out how to solve this problem. If anyone can give me some suggestions, I would greatly appreciate it. I am trying to read a log file and generate...
5
by: John Smith | last post by:
Can someone point me to an example of how to implement and access the kind of object shown below? Most of the examples if found are an object that contains one other object. I need to create an...
3
Chittaranjan
by: Chittaranjan | last post by:
Hi All, I am stuck with a problem in my coding that is the use of arrays and hashes in perl modules so any one can give me some link to good sites or examples to get deeper in array and hash in...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
4
by: lilly07 | last post by:
Hi, I have two text file as below: file1.txt (4 columns) test1 1000 2000 + test2 1000 2000 - test1 1000 2000 + test3 1000 2000 + test1 1000 2000 -
1
by: sixtyfootersdude | last post by:
Good Morning! I am a perl newbie and I think that I am struggling with references. I have an array of references to hashes which I am trying to print. This is what I have: for(my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.