#! 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 should 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{$categorycode} eq $search) { push(@category, $categorycode); } } print join(", ", @category) . "\n"; #### ## Facing problems for below serach and desired output print "What is Category code for Furniture | Output should be : FUR-BO 10001798? \n"; print "What is Category code for (Furniture Chairs) | Output should be : OFF-LA 10000240? \n"; print "What is Category code for (Office Supplies Storage) | Output should be : OFF-ST 10000107? \n"; print "What is Category code for (Office Supplies Art) | Output should be : OFF-AR 10003056? \n"; print "What is Category code for (Office Supplies Labels) | Output should be : OFF-LA 10000240? \n";