Kronheim, David (Contr) wrote:

Jim wrote:

At 12:26 PM +0800 3/22/12, lina wrote:

%abbrev = (
             'CNS Neurol Disord Drug Targets' =>  'CNSNDDT',
             'Intermolecular Forces' =>  'IF',
             'Expert Reviews in Molecular Medicine.' =>  'ERMM',
             'Nat. Neurosci.' =>  'NN',
             'Nat. Rev. Mol. Cell Biol.' =>  'NRMCB',
             'Nature Cell Biol.' =>  'NCB'
           );

My question is that how can I sort the values. I googled for a while.

sort $abbrev{$a} cmp $abbrev{$b} it complains that:

You need to put the sort test in a block:

for my $key ( sort { $abbrev{$a} cmp $abbrev{$b} } keys %abbrev ) {
   print "key=$key, value=$abbrev{$key}\n";
}

Since the default sort order, where items are sorted as strings in
ASCII sequence is not being changed, the following is sufficient:

for my $key (sort keys %abbrev) {
    print "key=$key, value=$abbrev{$key}\n";
}

Sorry, wrong. The example above is sorting the VALUES of the hash while your example is sorting the KEYS of the hash.


This communication is confidential.  Frontier only sends and receives
email on the basis of the terms set out at 
http://www.frontier.com/email_disclaimer.

This communication was sent to a public mailing list that is available world wide and as such can not be considered confidential.


John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to