Magnolia25 has asked for the wisdom of the Perl Monks concerning the following question:

I am facing problems with below code. This works with my search criteria 'Technology Phones' and returns correct key "TEC-PH 10001949" which is my requirements.

#! perl use strict; use warnings; use Data::Dumper; my %categories = ("FUR-BO 10001798" => 'Furniture', "FUR-CH 10000454" => [ 'Furniture' , 'Chairs'], "OFF-LA 10000240" => [ 'Office Supplies' , 'Labels'] +, "OFF-ST 10000107" => [ 'Office Supplies' , 'Storage' +], "OFF-AR 10003056" => [ 'Office Supplies' , 'Art'], "TEC-PH 10001949" => 'Technology Phones', ); #print Dumper(%categories); print "What is Category code for Technology Phones | Output s +hould be : TEC-PH 10001949? \n"; my $categorycode; my $search = 'Technology Phones'; my @category = (); foreach $categorycode (keys(%categories)) { if(ref($categories{$categorycode}) eq 'ARRAY' and grep { $_ eq $search } @{ $categories{$categorycode} }) { push(@category, $categorycode); } elsif(!ref($categories{$categorycode}) and $categories{$category +code} eq $search) { push(@category, $categorycode); } } print join(", ", @category) . "\n";

However if I change the search = 'Furniture' I need the output as "FUR-BO 10001798" But I get key value "FUR-BO 10001798" & "FUR-CH 10000454" which is not correct.

Also If I give search criteria as 'Office Supplies' , 'Labels' I want my output as "OFF-LA 10000240".

## Facing problems for below serach and desired output print "What is Category code for Furniture | Output s +hould be : FUR-BO 10001798? \n"; print "What is Category code for (Furniture Chairs) | Output s +hould be : OFF-LA 10000240? \n"; print "What is Category code for (Office Supplies Storage) | Output s +hould be : OFF-ST 10000107? \n"; print "What is Category code for (Office Supplies Art) | Output s +hould be : OFF-AR 10003056? \n"; print "What is Category code for (Office Supplies Labels) | Output s +hould be : OFF-LA 10000240? \n";

So $search variable are value/values in my hash and I need to return key as output.

Please help on fixing this. Thanks.

Replies are listed 'Best First'.
Re: return Hash key while hash value/values is search criteria
by poj (Abbot) on Jul 01, 2018 at 15:51 UTC

    Build a 'reverse' hash of category to code.

    #!perl use strict; use warnings; use Data::Dumper; my %categories = ("FUR-BO 10001798" => 'Furniture', "FUR-CH 10000454" => [ 'Furniture' , 'Chairs'], "OFF-LA 10000240" => [ 'Office Supplies' , 'Labels'] +, "OFF-ST 10000107" => [ 'Office Supplies' , 'Storage' +], "OFF-AR 10003056" => [ 'Office Supplies' , 'Art'], "TEC-PH 10001949" => 'Technology Phones', ); # build reverse hash text=>code my %code = (); for my $code (keys %categories){ my $cat = $categories{$code}; if ( ref($cat) eq 'ARRAY' ){ $cat = join ' ',@$cat; } if (exists $code{$cat}){ die "Duplicate code for $cat\n"; } else { $code{$cat} = $code }; } print Dumper \%code; my @items = ('Furniture', 'Furniture Chairs', 'Office Supplies Storage', 'Office Supplies Art', 'Office Supplies Labels'); search($_) for @items; sub search { my $text = shift; printf "Code for %-30s is %s\n",$text,$code{$text}; }
    poj

      Thanks it works

Re: return Hash key while hash value/values is search criteria
by NetWallah (Canon) on Jul 01, 2018 at 21:11 UTC
    poj's approach is best, but here is an alternative that produces desired results, if you insist on keeping the current data structure and search method:
    #! perl use strict; use warnings; use Data::Dumper; my %categories = ("FUR-BO 10001798" => 'Furniture', "FUR-CH 10000454" => [ 'Furniture' , 'Chairs'], "OFF-LA 10000240" => [ 'Office Supplies' , 'Labels'] +, "OFF-ST 10000107" => [ 'Office Supplies' , 'Storage' +], "OFF-AR 10003056" => [ 'Office Supplies' , 'Art'], "TEC-PH 10001949" => 'Technology Phones', ); my @items = ('Furniture', 'Furniture,Chairs', 'Technology Phones', 'Office Supplies,Storage', 'Office Supplies,Art', 'Office Supplies,Labels'); Search(split /,/,$_) for @items; exit 0; sub Search{ my (@search_words) = @_; print "Searching for @search_words ...\n"; my @category = (); foreach my $categorycode (keys(%categories)) { if(ref($categories{$categorycode}) eq 'ARRAY' ){ if ( $#{ $categories{$categorycode} } == $#search_words and join ("", @{ $categories{$categorycode}}) eq join + ("",@search_words)){ push @category, $categorycode; } next; } if(my $reftype = ref($categories{$categorycode})){ die "ERROR: Unxpected type: $reftype for item $categoryco +de"; } if( $categories{$categorycode} eq $search_words[0] and @sear +ch_words ==1) { push(@category, $categorycode); } } print join(", ", @category) . "\n"; }

                    Memory fault   --   brain fried

      if you insist on keeping the current data structure and search method:

      Thank you so much guessing me :-)

      Just in case if i want to map more values for same key within same data structure for e.g.

      key : "OFF-LA 10000240" values : 'Office SuppliesA' , 'Labels' 'Office SuppliesB' , 'Labels' 'Office SuppliesC' , 'Labels' 'Office SuppliesD' , 'Labels'

      I made this working by inverting hash though.

        #! perl use strict; use warnings; use Data::Dumper; my %categories = ("FUR-BO 10001798" => 'Furniture', "FUR-CH 10000454" => [ 'Furniture' , 'Chairs'], "OFF-LA 10000240" => [ 'Office Supplies' , 'Labels', 'Office SuppliesA' , 'Labels', 'Office SuppliesB' , 'Labels', 'Office SuppliesC' , 'Labels', 'Office SuppliesD' , 'Labels', ], "OFF-ST 10000107" => [ 'Office Supplies' , 'Storage' +], "OFF-AR 10003056" => [ 'Office Supplies' , 'Art'], "TEC-PH 10001949" => 'Technology Phones', ); my @items = ('Furniture', 'Furniture,Chairs', 'Technology Phones', 'Office Supplies,Storage', 'Office Supplies,Art', 'Office Supplies,Labels', map {"Office Supplies$_,Labels"} 'A'..'D' ); Search(split /,/,$_) for @items; exit 0; sub Search{ my (@search_words) = @_; print "Searching for @search_words ...\n"; my @category = (); foreach my $categorycode (keys(%categories)) { if(ref(my $carray = $categories{$categorycode}) eq 'ARRAY' ){ + for (my $idx=0; $idx < $#$carray; $idx +=2){ my @current = @$carray[$idx,$idx+1]; if ( $#current == $#search_words and join ("", @current) eq join ("",@search_words +)){ push @category, $categorycode; } } next; } if(my $reftype = ref($categories{$categorycode})){ die "ERROR: Unxpected type: $reftype for item $categoryco +de"; } if( $categories{$categorycode} eq $search_words[0] and @sear +ch_words ==1) { push(@category, $categorycode); } } print join(", ", @category) . "\n"; }

                        Memory fault   --   brain fried