问题描述
||
我想将值设为键,并将键设为值。这样做的最佳方法是什么?
解决方法
改编自http://www.dreamincode.net/forums/topic/46400-swap-hash-values/:
假设您的哈希存储在
$hash
中:
while (($key,$value) = each %hash) {
$hash2{$value}=$key;
}
%hash=%hash2;
似乎可以通过反向(http://www.misc-perl-info.com/perl-hashes.html#reverseph)实现更优雅的解决方案:
%nhash = reverse %hash;
请注意,如果反向,则重复值将被覆盖。
,使用reverse
:
use Data::Dumper;
my %hash = (\'month\',\'may\',\'year\',\'2011\');
print Dumper \\%hash;
%hash = reverse %hash;
print Dumper \\%hash;
,如前所述,最简单的是
my %inverse = reverse %original;
如果多个元素具有相同的值,则“失败”。您可以创建一个HoA来处理这种情况。
my %inverse;
push @{ $inverse{ $original{$_} } },$_ for keys %original;
,因此,您想在哈希表中使用反向键和值吗?所以使用反向...;)
%hash2 = reverse %hash;
还原(k1 => v1,k2 => v2)-产量(v2 => k2,v1 => k1)-这就是您想要的。 ;)
,my %orig_hash = (...);
my %new_hash;
%new_hash = map { $orig_hash{$_} => $_ } keys(%orig_hash);
,键上映射解决方案更加灵活。如果您的价值不是简单的价值怎么办?
my %forward;
my %reverse;
#forward is built such that each key maps to a value that is a hash ref:
#{ a => \'something\',b=> \'something else\'}
%reverse = map { join(\',\',@{$_}{qw(a b)}) => $_ } keys %forward;
,这是使用Hash::MultiValue
完成的方法。
use experimental qw(postderef);
sub invert {
use Hash::MultiValue;
my $mvh = Hash::MultiValue->from_mixed(shift);
my $inverted;
$mvh->each( sub { push $inverted->{ $_[1] }->@*,$_[0] } ) ;
return $inverted;
}
要对此进行测试,我们可以尝试以下方法:
my %test_hash = (
q => [qw/1 2 3 4/],w => [qw/4 6 5 7/],e => [\"8\"],r => [\"9\"],t => [\"10\"],y => [\"11\"],);
my $wow = invert(\\%test_hash);
my $wow2 = invert($wow);
use DDP;
print \"\\n \\%test_hash:\\n\\n\" ;
p %test_hash;
print \"\\n \\%test_hash inverted as:\\n\\n\" ;
p $wow ;
# We need to sort the contents of the multi-value array reference
# for the is_deeply() comparison:
map {
$test_hash{$_} = [ sort { $a cmp $b || $a <=> $b } @{ $test_hash{$_} } ]
} keys %test_hash ;
map {
$wow2->{$_} = [ sort { $a cmp $b || $a <=> $b } @{ $wow2->{$_} } ]
} keys %$wow2 ;
use Test::More ;
is_deeply(\\%test_hash,$wow2,\"double inverted hash == original\");
done_testing;
附录
请注意,为了通过此处的花哨测试,invert()
函数依赖于array14ѭ将数组引用作为值。要解决此问题,如果您的哈希值不是数组引用,则可以将常规/混合哈希“强制”为多值哈希,然后将“ 10”加成对象。但是,这种方法意味着即使单个值也将作为数组引用出现:
for ( keys %test_hash ) {
if ( ref $test_hash{$_} ne \'ARRAY\' ) {
$test_hash{$_} = [ $test_hash{$_} ]
}
}
这对于:
ref($_) or $_ = [ $_ ] for values %test_hash ;
只有通过“往返”测试才能通过。