#! 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 $categorycode"; } if( $categories{$categorycode} eq $search_words[0] and @search_words ==1) { push(@category, $categorycode); } } print join(", ", @category) . "\n"; }