在 Perl 语言中,我将散列定义为一个事物与另一个事物之间的映射,或者一个基本元素列表。如文档中所述。 哈希是一种基本数据类型。它使用密钥来访问其内容。
所以基本上哈希接近数组。他们的初始化甚至看起来非常相似。
如果我要在 Perl 中创建一个映射,我可以做如下的比较。
my %map = (
A => [qw(a b c d)],
B => [qw(c d f a)],
C => [qw(b d a e)],
);
my @keys = keys %map;
my %matches;
for my $k ( 1 .. @keys ) {
$matches{$_} |= 2**$k for @{$map{ $keys[$k-1] }};
}
for ( sort keys %matches ) {
my @found;
for my $k ( 1 .. @keys ) {
push @found, $keys[$k-1] if $matches{$_} & 2**$k;
}
print "$_ found in ", (@found? join(',', @found) : 0 ), "\n";
}
Output:
a found in A,C,B
b found in A,C
c found in A,B
d found in A,C,B
e found in C
f found in B