Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 03-10-2005, 04:51 AM   #1
Zester
Confirmed User
 
Zester's Avatar
 
Industry Role:
Join Date: Jul 2003
Posts: 5,344
how to (really) sort a hash by key in Perl

lets I have a hash:

%hash=(
'2004' => "red",
'2006' => "blue",
'2005' => "green"
);

how do I sort it by key? i don't want to print it out sorted, only sort it

using this:
@keys = sort keys %hash;

will NOT work, because @keys now contain only the keys
why is it so fucking hard to get this:

%hash=(
'2004' => "red",
'2005' => "green",
'2006' => "blue"
);
__________________
* Mainstream ? $65 per sale
* new male contraception

Last edited by Zester; 03-10-2005 at 04:53 AM..
Zester is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-10-2005, 04:57 AM   #2
Phil21
Confirmed User
 
Join Date: May 2001
Location: ICQ: 25285313
Posts: 993
You cannot "keep" a hash sorted, it's simply not how hashes work.

Do what you did w/ array storing the keys. Then foreach that array when you want to perform an ordered operation on the hash. Depending on the application of course there are better ways, but considering what posts you've made on the subject this should be fine. Arrays will keep their order of course.

Just do something like..

foreach my $key (@keys) {
my $value=$hash{$key};
print "key $key - value $value\n";
...
}

Shrug..
__________________
Quality affordable hosting.
Phil21 is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-10-2005, 05:15 AM   #3
teksonline
So Fucking Banned
 
Join Date: Jan 2005
Location: At My Desk
Posts: 2,904
foreach $code (sort { $countries{$a} cmp $countries{$b} } keys %countries) { do this...
teksonline is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-10-2005, 09:08 AM   #4
WiredGuy
Pounding Googlebot
 
Industry Role:
Join Date: Aug 2002
Location: Canada
Posts: 34,482
You can't actually sort the hash but as teksonline said you can have the ordering permutted with that code and do whatever you want at runtime. The actual data can't be sorted like an array though.

WG
__________________
I play with Google.
WiredGuy is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-10-2005, 12:01 PM   #5
Big E
Registered User
 
Industry Role:
Join Date: Mar 2002
Location: San Diego, CA
Posts: 935
Quote:
Originally Posted by teksonline
foreach $code (sort { $countries{$a} cmp $countries{$b} } keys %countries) { do this...
FYI - That sorts by VALUE, not KEY.

foreach $Key ( sort %hash ) {
print "$Key => " . $hash{$Key} . "\n";
}
Big E is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.