让您失望的是您的哈希值不是实际值,而是[ ... ]
指向包含实际数据的单元素数组的数组引用(如 Data::Dumper 输出中所示)。所以你需要取消引用它们并从结果数组中获取第一个元素:
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
my %hash1 = ('5555' => [ '13570' ]);
my %hash2 = ('13570' => [ '[04/Jun/2013:15:06:13' ]);
for (keys %hash1) {
my $first_key = $_;
my $second_key = $hash1{$first_key}[0];
say "$first_key -> $second_key -> $hash2{$second_key}[0]";
}
输出:
5555 -> 13570 -> [04/Jun/2013:15:06:13
编辑: 备用代码检查每个条目中的所有值%hash1
并显示每个条目中的所有对应值%hash2
:
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
my %hash1 = ('5555' => [ '13570', '8675309' ]);
my %hash2 = (
'13570' => [ '[04/Jun/2013:15:06:13' ],
8675309 => [ 'Jenny', 'I got your number' ],
);
for (keys %hash1) {
my $first_key = $_;
for my $second_key ( @{$hash1{$first_key}} ) {
if (exists $hash2{$second_key}) {
say "$first_key -> $second_key ->";
say "\t$_" for @{$hash2{$second_key}};
}
}
}
输出:
5555 -> 13570 ->
[04/Jun/2013:15:06:13
5555 -> 8675309 ->
Jenny
I got your number