1) %hash = (
1 => 'a',
4 => 'g',
7 => 'h'
);
WAP在不使用反向函数的情况下反转给定的哈希值?
答案 0 :(得分:2)
你应该学习Perl - 你自己学习它真的很有趣。
真的很简单:
my %rev;
foreach my $key (keys %hash) {
$rev{$hash{$key}} = $key;
}
答案 1 :(得分:0)
TIMTOWTDI:
my %a = ( foo => 1, bar => 2 ); # original hash
my @a = %a; # copy to array
my @rev; # reversed list container
push @rev, pop @a while @a; # fill @rev from the back forward
my %rev = @rev; # assign reversed list to hash
答案 2 :(得分:0)
TIMTOWTDI II:
my %rev =
map {$hash{$_} => $_}
keys %hash;
答案 3 :(得分:0)
my %by_letter;
@by_letter{ values(%by_num) } = keys(%by_num);
或
my %by_letter = map { $by_num{$_} => $_ } keys(%by_num);