in reply to
hash sorting/alphabetization issue : country postal codes
Send sort the hash values not the keys.
#!/usr/bin/perl
use warnings;
use strict;
my %country_names = (
"AF" => "Afghanistan",
"AX" => "Aland Islands",
"AL" => "Albania",
"DZ" => "Algeria",
"AS" => "American Samoa",
"AD" => "Andorra",
"AO" => "Angola",
"AI" => "Anguilla",
"AQ" => "Antarctica",
"AG" => "Antigua And Barbuda",
"AR" => "Argentina",
"AM" => "Armenia",
"AW" => "Aruba",
"AU" => "Australia",
"AT" => "Austria",
"AZ" => "Azerbaijan",
"BS" => "Bahamas",
"BH" => "Bahrain",
"BD" => "Bangladesh",
"BB" => "Barbados",
"ZZ" => "AAAAAAA"
);
my @sorted_keys = sort { $country_names{$a} cmp $country_names{$b} } k
+eys %country_names;
print join("\n",@sorted_keys);
Re^2: hash sorting/alphabetization issue : country postal codes by hmbscully (Scribe) on Jul 03, 2007 at 19:59 UTC |
| [reply] |
|
Hashes are not ordered (well in any order you care about). So I don't quite know what you are referring to.
but the problem is that the option items are in alphabetical order based on the country codes, not the country names.
Am I not understanding what you meant by that?
| [reply] |
Re^2: hash sorting/alphabetization issue : country postal codes by parv (Priest) on Jul 03, 2007 at 20:24 UTC |
my @sorted_keys = sort { $country_names{$a} cmp $country_names{$b} } k
+eys %country_names;
Considering that in the end you would have sorted VALUES not the keys, above array variable name is rather misleading.
Correction. Afer grep's reply below, I saw that what was misleading was my claim made above. The array name was reasonable; it was my fault for forgetting that sort sorted the LIST based on result of BLOCK.
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
|
Oh! Right you were, and are. Indeed the array name is reasonable and the array does contain sorted keys not the values. I apologise for distrubing the peace.
I will stand in a corner now for a while ...
| [reply] |