Printing a hash question

S

scadav

I am new to Perl and this problem is baffling me. I undestand how to
print out all the items in a hash and sort by the keys:

foreach $item (sort keys(%hash)) {
print "$item: $hash{$item} \n";
}

but how do you do the reverse of this?...print out all of the items
but sort on the values?

any help would be appreciated.
 
G

Gunnar Hjalmarsson

scadav said:
I am new to Perl and this problem is baffling me. I undestand how
to print out all the items in a hash and sort by the keys:

foreach $item (sort keys(%hash)) {
print "$item: $hash{$item} \n";
}

but how do you do the reverse of this?...print out all of the items
but sort on the values?

That's a Frequently Asked Question:

perldoc -q "sort a hash"

You are supposed to check the Perl FAQ before posting questions here.

http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
J

John Bokma

scadav said:
I am new to Perl and this problem is baffling me. I undestand how to
print out all the items in a hash and sort by the keys:

foreach $item (sort keys(%hash)) {
print "$item: $hash{$item} \n";
}

but how do you do the reverse of this?...print out all of the items
but sort on the values?


for my $key ( sort { $hash{$a} cmp $hash{$b} } keys %hash ) {

}
 
T

Tad McClellan

scadav said:
print out all of the items
but sort on the values?

any help would be appreciated.


Any checking of the Perl FAQ *before* posting to the
Perl newsgroup appreciated.


perldoc -q hash
perldoc -q sort
 
E

Eric Bohlman

for my $key ( sort { $hash{$a} cmp $hash{$b} } keys %hash ) {

}

It's a bad idea to try to answer Frequently Asked Questions yourself rather
than referring the poster to the FAQ. IIRC, last week a well-known regular
(I forget who) let his guard down and tried to answer a FAQ himself, but in
the process he made a mistake. Another regular posted a correction, but
it's quite possible that the original poster, not to mention any number of
lurkers, stopped reading the thread as soon as they saw what looked like an
answer and missed the correction (the correction may not have made it to
their news server yet).

Someone who asks FAQs is, almost by definition, someone who doesn't yet
know enough about Perl to be able to tell whether or not he's getting a bum
steer. There's very little chance that the Perl FAQ will give them a bum
steer. There's a much greater chance that a Usenet poster will
accidentally do so.
 
J

John Bokma

It's a bad idea to try to answer Frequently Asked Questions yourself rather

Sometimes postings like the above are added to the FAQ, or can be a
correction on the FAQ.

OTOH I agree with you too ;-)
the process he made a mistake. Another regular posted a correction, but

No problem there. I see often corrections and/or additions by people
here, wether it are FAQ questions or not. It keeps one sharp, and one
learns.
it's quite possible that the original poster, not to mention any number of
lurkers, stopped reading the thread as soon as they saw what looked like an
answer and missed the correction (the correction may not have made it to
their news server yet).

So they have a non-working solution. Either they correct it themself and
learn from it or they come back for more.

You mistake Usenet with a free helpdesk and solution providing thingy.
Someone who asks FAQs is, almost by definition, someone who doesn't yet
know enough about Perl to be able to tell whether or not he's getting a bum
steer. There's very little chance that the Perl FAQ will give them a bum
steer.

Someone who just copy-pastes code will run into many problems. Wether
the FAQ is the source or not. The FAQ will rarely provide what they
exactly want.
There's a much greater chance that a Usenet poster will
accidentally do so.

And nothing wrong there. The answers are given for free.

I see that there is a sorting question ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top