in reply to Bi-Directional Hash Lookup
This sounds like a job for overload.
## untested "idea" code ## package TwoWayHash; use overload '%{}' => \&as_hash; sub new { my $self = shift; my $hashref = shift; my %struct = ( 'data' => $hashref ); bless \%struct, $self; } sub as_hash { my $self = shift; # this will only work for simple hashes, really my $temp = reverse $self->{data}; return $temp{shift}; }
Which in turn might be used as:
use TwoWayHash; my %a = get_results_from_query; # assume this populates %a; # then, when you need $b{$a}: { my $b = TwoWayHash->(\%a); print "I need ",$$b{'item'}; ## finds key where $a{key} eq 'item' }
Just ideas, not tested, to get you thinking down this type of road. You could also do this with a function that implements the logic of as_hash(), and then merely call it like:
print "I need", rv_lookup('item',\%a);
|
|---|