in reply to Perl style... help me figure this out.

jaydstein:

If you're trying to determine the category of a particular item, and you're doing it frequently in your code, then you might want to use a reverse lookup table (inverted hash). Basically, it's just a hash where each value points to the category name in your list:

$ cat 952190.pl #!/usr/bin/perl use strict; use warnings; my $hash = { 'COOKIES' => ['CHOCOLATE CHIP', 'PEANUT BUTTER', 'OATMEAL'], 'METALS' => ['SILVER', 'GOLD', 'PLATINUM'], 'BREAKFAST' => ['OATMEAL', 'WAFFLES', 'CEREAL'], }; my $values = { 'COOKIES' => '1', 'SILVER' => '1', }; my $revLookup; for my $k (%$hash) { for my $v (@{$$hash{$k}}) { $$revLookup{$v} = $k; } } print who_do_ya_love('CHOCOLATE CHIP'); print who_do_ya_love('PLATINUM'); print who_do_ya_love('OATMEAL'); sub who_do_ya_love { my $item = shift; my $category = $$revLookup{$item} // 'NOTHING'; "This guy loves $category\n"; } $ perl 952190.pl This guy loves COOKIES This guy loves METALS This guy loves BREAKFAST

The downsides of using an inverted hash:

...roboticus

When your only tool is a hammer, all problems look like your thumb.