1) %hash = (
1 => 'a',
4 => 'g',
7 => 'h'
);
WAP 在不使用反向功能的情况下反转给定的哈希?
你真的应该学习 Perl - 当你自己学习它时真的很有趣。
真的就这么简单:
my %rev;
foreach my $key (keys %hash) {
$rev{$hash{$key}} = $key;
}
蒂姆托迪:
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
my %by_letter;
@by_letter{ values(%by_num) } = keys(%by_num);
或者
my %by_letter = map { $by_num{$_} => $_ } keys(%by_num);