簡體   English   中英

如何在不使用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是否不存在並打印“內容”,但我也希望它也檢查“ a”鍵以查看是否具有“ AUS”值。 那么,如何檢查整個哈希中是否存在$ name(我無法通過鍵值對使用find,因為我試圖在每一列中查找和打印)?

這里沒有魔術子彈。 您必須遍歷哈希並檢查每個值。 有多種方法可以執行此操作,而您所使用的方法則取決於散列源的填充方式。

遞歸解決方案是:

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

因為這是遞歸的,所以它將遍歷哈希的任何“深度”,但這可能並不完全適合您的用例。

但是,您在談論列的事實向我表明,此哈希值是從其他地方填充的-我建議您查看該填充過程,因為很有可能是一個更好的位置來開始選擇特定的計數價值觀。

如果您需要更通用的查找表:

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

如果我理解正確,那么您想要這樣的事情:

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