简体   繁体   English

如何在不使用Perl中的键的情况下查找值是否存在于哈希中?

[英]How to find if the value exists in hash without using key in perl?

I have an hash map like this 我有这样的哈希图

my $name = 'AUS'; #dynamic values
my %hash = { 'a'=>{
                  'x'=> {
                         '1' =>'US'
                         '2' =>'UK'
                        }
                  'y'=>{
                          '1' =>'AFRICA'
                          '2' =>'AUS'
                       }
                   }
            'b'=>{
                   'x' =>{
                           '1' =>'US'
                           '2' =>'UK'
                         }
                 }
           };

I am trying to find whether name is unique in the hash for each column 我正在尝试查找每个列的哈希中名称是否唯一

foreach my $key(keys %hash)
{
   if($name ne $hash{}{}{}) #is name unique in whole hash?
   {
      print "something";
   }
   else
   {
      print "nothing";
   }
}

All is fine but when it comes to key 'b' it checks that AUS is not present and prints "something" but I want it to check the 'a' key too to see if has 'AUS' value. 一切都很好,但是当涉及到键“ b”时,它会检查AUS是否不存在并打印“内容”,但我也希望它也检查“ a”键以查看是否具有“ AUS”值。 So,how to check whether $name exists in whole hash (i can't use find via key-value pair since i am trying to find and print in each column) ? 那么,如何检查整个哈希中是否存在$ name(我无法通过键值对使用find,因为我试图在每一列中查找和打印)?

There's no magic bullet here. 这里没有魔术子弹。 You have to traverse your hash and inspect each value. 您必须遍历哈希并检查每个值。 There's a variety of approaches to doing this, and which you use is rather dependent on how your hash-source gets populated. 有多种方法可以执行此操作,而您所使用的方法则取决于散列源的填充方式。

A recursive solution would be: 递归解决方案是:

#!/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; 

Because this is recursive, it will walk to any 'depth' of the hash, but that might not be entirely appropriate for you use-case. 因为这是递归的,所以它将遍历哈希的任何“深度”,但这可能并不完全适合您的用例。

However the fact that you're talking about columns, suggests to me that this hash is being populated from elsewhere - I would suggest you look at that population process, because it's quite likely that's a better place to start picking out particular counts-of-values. 但是,您在谈论列的事实向我表明,此哈希值是从其他地方填充的-我建议您查看该填充过程,因为很有可能是一个更好的位置来开始选择特定的计数价值观。

If you need a more versatile lookup 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};

if I understand correctly you want something like this: 如果我理解正确,那么您想要这样的事情:

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM