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

后端 未结 2 743
情话喂你
情话喂你 2021-01-28 02:11

I have an hash map like this

my $name = \'AUS\'; #dynamic values
my %hash = { \'a\'=>{
                  \'x\'=> {
                         \'1\' =>\'US         


        
2条回答
  •  心在旅途
    2021-01-28 02:42

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

提交回复
热议问题