Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: Creating a hash from arrays

by BrowserUk (Pope)
on Nov 02, 2013 at 22:58 UTC ( #1060989=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Creating a hash from arrays
in thread Creating a hash from arrays

but I can't get it to work if I call sort on values.

You need to create an array of the keys ordered by the associated values:

[0] Perl> @a{ 1..26 } = reverse 'a'..'z';; @ordered = sort{ $a{$a} cmp $a{$b} } keys %a;; print "$_ :: $a{ $_ }" for @ordered;; 26 :: a 25 :: b 24 :: c 23 :: d 22 :: e 21 :: f 20 :: g 19 :: h 18 :: i 17 :: j 16 :: k 15 :: l 14 :: m 13 :: n 12 :: o 11 :: p 10 :: q 9 :: r 8 :: s 7 :: t 6 :: u 5 :: v 4 :: w 3 :: x 2 :: y 1 :: z

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re^3: Creating a hash from arrays
Download Code
Re^4: Creating a hash from arrays
by cspctec (Sexton) on Nov 02, 2013 at 23:25 UTC
    I still can't get it to sort by values. Sorting by keys seems to be easy, but it looks like values were not meant to be sorted by.

      Please show your code. (Update: And also take a look at BrowserUk's code. (Update: Oops... Just noticed your post was already a reply to BrowserUk's code post. Well, taking another look would not be wasted effort.))

      I still can't get it to sort by values. Sorting by keys seems to be easy, but it looks like values were not meant to be sorted by.

      No, absolutely not. Sorting by values is OK, as shown by examples above. If you don't get it right, then it means you are simply not doing right.

      This is an example built on the code proposed by Kenosis. I have changed and added a couple of names to make sure that the states don't get in the right order just by chance (which was the case with the original data you gave):

      use strict; use warnings; use Data::Dumper; my @fname = ( "Philip", "John", "Daniel", "John", "Ted" ); my @lname = ( "Johnson", "Hill", "Sharp", "Evans", "McCain" ); my @state = ( "AK", "CA", "AL", "WI", "TX"); my %hash = map { $fname[$_] . ' ' . $lname[$_] => $state[$_] } 0 .. $# +state; print "Unsorted:\n" ,Dumper \%hash; print "==========\n"; print "Sorted:\n", map "$_, $hash{$_}\n", sort {$hash{$a} cmp $hash{$b +}} keys %hash;
      which gives the following output:
      $ perl sort_hash.pl Unsorted: $VAR1 = { 'John Evans' => 'WI', 'Philip Johnson' => 'AK', 'John Hill' => 'CA', 'Ted McCain' => 'TX', 'Daniel Sharp' => 'AL' }; ========== Sorted: Philip Johnson, AK Daniel Sharp, AL John Hill, CA Ted McCain, TX John Evans, WI
      As you can see, the data is now sorted by the values (i.e. the State).

      If you still don't succeed to do it, please show your code.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2014-11-24 04:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (135 votes), past polls