Arrays 如何比较具有哈希记录的数组并检索匹配值?(来自键/值对)
孤儿是存储用户名的数组。我必须 比较孤立记录并从hash=>hash2i中找出匹配的键 我能够比较并获取hash2中存在的密钥 但是,当我试图检索相应的 价值观 示例数据:Hash2包含=>{ASE,UK}{CCY,US}{PSD,US}{ZK9,UL} 孤立项包含=>ZK9,ASEArrays 如何比较具有哈希记录的数组并检索匹配值?(来自键/值对),arrays,perl,hash,Arrays,Perl,Hash,孤儿是存储用户名的数组。我必须 比较孤立记录并从hash=>hash2i中找出匹配的键 我能够比较并获取hash2中存在的密钥 但是,当我试图检索相应的 价值观 示例数据:Hash2包含=>{ASE,UK}{CCY,US}{PSD,US}{ZK9,UL} 孤立项包含=>ZK9,ASE 我是这样写的: foreach(@orphans1) { if(exists ($hash2{$_})) { my $values = values %ha
我是这样写的:
foreach(@orphans1)
{
if(exists ($hash2{$_}))
{
my $values = values %hash2{$_};
print "\n\n YES INSIDE THE LOOP: $values";
}
}
下一个:
In hash! key: ZK9 value: UL
In hash! key: ASE value: UK
印刷品
use 5.010;
use warnings;
use Data::Dumper;
my @orp = qw(ZK9 ASE);
my %hash = ('ASE' => 'UK', 'CCY' => 'US', 'PSD' => 'US', 'ZK9' => 'UL');
my @o = @hash{@orp}; #values hash slice
say "@o";
use 5.020; #need 5.20
use warnings;
use Data::Dumper;
my @orp = qw(ZK9 ASE);
my %hash = ('ASE' => 'UK', 'CCY' => 'US', 'PSD' => 'US', 'ZK9' => 'UL');
my %h = %hash{@orp}; #key/value hash slice
say Dumper \%h;
这就是你想要的吗
或:
印刷品
use 5.010;
use warnings;
use Data::Dumper;
my @orp = qw(ZK9 ASE);
my %hash = ('ASE' => 'UK', 'CCY' => 'US', 'PSD' => 'US', 'ZK9' => 'UL');
my @o = @hash{@orp}; #values hash slice
say "@o";
use 5.020; #need 5.20
use warnings;
use Data::Dumper;
my @orp = qw(ZK9 ASE);
my %hash = ('ASE' => 'UK', 'CCY' => 'US', 'PSD' => 'US', 'ZK9' => 'UL');
my %h = %hash{@orp}; #key/value hash slice
say Dumper \%h;
values
返回散列中的值列表,将其分配给标量。这几乎肯定不是你想要的——它只会给你一个%hash2{$}
$VAR1={'ZK9'=>'UL','ASE'=>'UK'中的值计数@SalyP检查河豚的回答我无法获得相同的输出!事实上,它没有在散列中找到匹配项。
$VAR1 = {
'ZK9' => 'UL',
'ASE' => 'UK'
};