in reply to Re^2: return Hash key while hash value/values is search criteria
in thread return Hash key while hash value/values is search criteria
#! 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
|
|---|