Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

auto-magical flipped hashes

by gregorovius (Friar)
on Jul 12, 2000 at 22:31 UTC ( [id://22237]=perlquestion: print w/replies, xml ) Need Help??

gregorovius has asked for the wisdom of the Perl Monks concerning the following question:

%h = (key1 => 'content1', key2 => 'content2'); $h{FLIPPED} = { map {$h{$_}, $_ } (keys %h) }; print "$h{key1} : $h{FLIPPED}{content1}\n";
Wouldn't it be nice if perl provided 'FLIPPED' as a magical hash key to automatically make available the flipped contents of a hash? Is there a simpler way to do this?

Replies are listed 'Best First'.
Re: auto-magical flipped hashes
by btrott (Parson) on Jul 12, 2000 at 22:39 UTC
    perlfaq4: How do I look up a hash element by value?:
    Create a reverse hash: %by_value = reverse %by_key; $key = $by_value{$value}; That's not particularly efficient. It would be more space-efficient to use: while (($key, $value) = each %by_key) { $by_value{$value} = $key; } If your hash could have repeated values, the methods above will only find one of the associated keys. This may or may not worry you.
    You could probably use tie to do this magically, using your FLIPPED key.
    package SpecialHash; use Tie::Hash; @SpecialHash::ISA = qw/Tie::StdHash/; sub FETCH { $_[1] eq "FLIPPED" ? { reverse %{ $_[0] } } : $_[0]->{$_[1]}; } package main; tie my %hash, 'SpecialHash'; @hash{ qw/foo bar/ } = qw/baz quux/; use Data::Dumper; print Dumper $hash{FLIPPED};
RE: auto-magical flipped hashes
by cariaso (Novice) on Jul 13, 2000 at 00:18 UTC
    The previous comment shows a good way to do this, but hashes with duplicate values would present a problem. %hash = ('a' => 1, 'b' => 1, ); Would you want to eliminiate one of the keys %flipped = ('1' => 'b'); or would you want them converted to a more complex structure? %flipped = ('1' => 'a', 'b');
      Very good point. This (not very optimised) code would flip and create the data structure. It is a bit cheesy though:
      use Data::Dumper; my %hash = ( 'a'=>1, 'b'=>2, 'c'=>3, 'd'=>4, 'e'=>4, 'f'=>4); my %by_value = flip(%hash); print Data::Dumper->Dump([\%hash], ['*hash']); print Data::Dumper->Dump([\%by_value], ['*by_value']); sub flip { my($k, $v, %hash); my %foo = @_; $hash{$v} = defined $hash{$v} ? [ ref $hash{$v} ? @{$hash{$v}} : $hash{$v} , $k ] +: $k while (($k,$v) = each %foo); return %hash; }
      Results:
      %hash = ( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 4, 'f' => 4 ); %by_value = ( 1 => 'a', 2 => 'b', 3 => 'c', 4 => [ 'd', 'e', 'f' ] );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://22237]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 13:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found