如何在不使用perl中的键的情况下查找散列中是否存在该值?
How to find if the value exists in hash without using key in perl?
我有一个这样的哈希映射
my $name = 'AUS'; #dynamic values
my %hash = { 'a'=>{
'x'=> {
'1' =>'US'
'2' =>'UK'
}
'y'=>{
'1' =>'AFRICA'
'2' =>'AUS'
}
}
'b'=>{
'x' =>{
'1' =>'US'
'2' =>'UK'
}
}
};
我正在尝试查找名称在每一列的散列中是否唯一
foreach my $key(keys %hash)
{
if($name ne $hash{}{}{}) #is name unique in whole hash?
{
print "something";
}
else
{
print "nothing";
}
}
一切都很好,但是当涉及到密钥 'b' 时,它会检查 AUS 是否存在并打印 "something" 但我希望它也检查 'a' 密钥以查看是否具有 'AUS' 值。那么,如何检查 $name 是否存在于整个哈希中(我无法通过键值对使用查找,因为我试图在每一列中查找和打印)?
如果我没理解错你想要这样的东西:
use strict;
use warnings;
my $name = 'AUS'; #dynamic values
my %hash = ( 'a'=>{
'x'=> {
'1' =>'US',
'2' =>'UK'
},
'y'=>{
'1' =>'AFRICA',
'2' =>'AUS'
}
},
'b'=>{
'x' =>{
'1' =>'US',
'2' =>'UK'
}
}
);
my @val = grep {$_ eq $name} map {my $x=$_; map {my $y=$_; map {$hash{$x}->{$y}->{$_}} keys %{$hash{$x}->{$_}}} keys %{$hash{$_}}} keys %hash;
if(@val == 0) {
print "$name not found";
}
elsif(@val == 1) {
print "$name is unique";
}
else {
print "$name is not unique";
}
这里没有灵丹妙药。您必须遍历哈希并检查每个值。有多种方法可以做到这一点,您使用哪种方法取决于散列源的填充方式。
递归解决方案是:
#!/usr/bin/env perl
use strict;
use warnings;
my $name = 'AUS';
use Data::Dumper;
my %hash = ( 'a'=>{
'x'=> {
'1' =>'US',
'2' =>'UK'
},
'y'=>{
'1' =>'AFRICA',
'2' =>'AUS'
}
},
'b'=>{
'x' =>{
'1' =>'US',
'2' =>'UK'
}
}
);
my %count_of;
sub traverse {
my ( $input_hash ) = @_;
foreach my $sub ( values %{$input_hash} ) {
if (ref $sub) {
traverse ($sub);
}
else {
$count_of{$sub}++;
}
}
}
traverse (\%hash);
print Dumper \%count_of;
print "$name is unique\n" if $count_of{$name} == 1;
因为这是递归的,它会遍历任何 'depth' 的散列,但这可能不完全适合您的用例。
但是,您谈论的是列这一事实向我表明,此哈希是从其他地方填充的 - 我建议您查看该填充过程,因为这很可能是开始挑选的更好位置特定的值计数。
如果您需要更通用的查询table:
my @unique_elements = grep { $count_of{$_} == 1 } sort keys %count_of;
print Dumper \@unique_elements;
my %is_unique = map { $_ => 1 } @unique_elements;
print Dumper \%is_unique;
print "$name is unique\n" if $is_unique{$name};
我有一个这样的哈希映射
my $name = 'AUS'; #dynamic values
my %hash = { 'a'=>{
'x'=> {
'1' =>'US'
'2' =>'UK'
}
'y'=>{
'1' =>'AFRICA'
'2' =>'AUS'
}
}
'b'=>{
'x' =>{
'1' =>'US'
'2' =>'UK'
}
}
};
我正在尝试查找名称在每一列的散列中是否唯一
foreach my $key(keys %hash)
{
if($name ne $hash{}{}{}) #is name unique in whole hash?
{
print "something";
}
else
{
print "nothing";
}
}
一切都很好,但是当涉及到密钥 'b' 时,它会检查 AUS 是否存在并打印 "something" 但我希望它也检查 'a' 密钥以查看是否具有 'AUS' 值。那么,如何检查 $name 是否存在于整个哈希中(我无法通过键值对使用查找,因为我试图在每一列中查找和打印)?
如果我没理解错你想要这样的东西:
use strict;
use warnings;
my $name = 'AUS'; #dynamic values
my %hash = ( 'a'=>{
'x'=> {
'1' =>'US',
'2' =>'UK'
},
'y'=>{
'1' =>'AFRICA',
'2' =>'AUS'
}
},
'b'=>{
'x' =>{
'1' =>'US',
'2' =>'UK'
}
}
);
my @val = grep {$_ eq $name} map {my $x=$_; map {my $y=$_; map {$hash{$x}->{$y}->{$_}} keys %{$hash{$x}->{$_}}} keys %{$hash{$_}}} keys %hash;
if(@val == 0) {
print "$name not found";
}
elsif(@val == 1) {
print "$name is unique";
}
else {
print "$name is not unique";
}
这里没有灵丹妙药。您必须遍历哈希并检查每个值。有多种方法可以做到这一点,您使用哪种方法取决于散列源的填充方式。
递归解决方案是:
#!/usr/bin/env perl
use strict;
use warnings;
my $name = 'AUS';
use Data::Dumper;
my %hash = ( 'a'=>{
'x'=> {
'1' =>'US',
'2' =>'UK'
},
'y'=>{
'1' =>'AFRICA',
'2' =>'AUS'
}
},
'b'=>{
'x' =>{
'1' =>'US',
'2' =>'UK'
}
}
);
my %count_of;
sub traverse {
my ( $input_hash ) = @_;
foreach my $sub ( values %{$input_hash} ) {
if (ref $sub) {
traverse ($sub);
}
else {
$count_of{$sub}++;
}
}
}
traverse (\%hash);
print Dumper \%count_of;
print "$name is unique\n" if $count_of{$name} == 1;
因为这是递归的,它会遍历任何 'depth' 的散列,但这可能不完全适合您的用例。
但是,您谈论的是列这一事实向我表明,此哈希是从其他地方填充的 - 我建议您查看该填充过程,因为这很可能是开始挑选的更好位置特定的值计数。
如果您需要更通用的查询table:
my @unique_elements = grep { $count_of{$_} == 1 } sort keys %count_of;
print Dumper \@unique_elements;
my %is_unique = map { $_ => 1 } @unique_elements;
print Dumper \%is_unique;
print "$name is unique\n" if $is_unique{$name};