Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: hash sorting/alphabetization issue : country postal codes

by grep (Monsignor)
on Jul 03, 2007 at 19:40 UTC ( #624761=note: print w/ replies, xml ) Need Help??


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);

Comment on Re: hash sorting/alphabetization issue : country postal codes
Select or Download Code
Re^2: hash sorting/alphabetization issue : country postal codes
by hmbscully (Scribe) on Jul 03, 2007 at 19:59 UTC
    The hash is already in alphabetical order by the values. I sorted those first thing.

    I learn more and more about less and less until eventually I know everything about nothing.
      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?

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.

      Considering that in the end you would have sorted VALUES not the keys, above array variable name is rather misleading.

      I disagree - @sorted_keys contains SORTED KEYS, sorted by the values.
      But feel free to call it @keys_sorted_by_value if you feel like it.

        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 ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://624761]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-12-26 15:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls