ׁHi Lina,
On Wed, 21 Mar 2012 21:00:53 +0800
lina <[email protected]> wrote:
> Hi,
>
> I tried to write something, but chocked in the end,
>
> Thanks ahead for your advice,
>
> #!/usr/bin/env perl
>
> use strict;
> use warnings;
>
> my %h = (
> 1 => "a",
> 2 => "b",
> 3 => "c"
> );
>
> foreach my $key ( sort keys %h){
> $h{$h{$key}}=$key;
> delete $h{$key};
> }
This is a very bad and dangerous way to do this.
A better way would be:
my %reversed_h = reverse(%h);
If you need more fine-grained control on the output of the reversed hash, then
loop on the keys into a new hash - don't modify the existing hash in-place.
> foreach my $key (sort keys %h){
> print $array{$key},"\n";
Where does %array come from?
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Apple Inc. is Evil - http://www.shlomifish.org/open-source/anti/apple/
Trying to block Internet pornography is like climbing a waterfall and trying
to stay dry. (— one of Shlomi Fish’s Internet Friends)
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/