Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

auto-magical flipped hashes

by gregorovius (Friar)
on Jul 12, 2000 at 22:31 UTC ( #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
Node Status?
node history
Node Type: perlquestion [id://22237]
Approved by root
help
Chatterbox?
[ambrus]: ad code examples.
[ambrus]: I'm not trying to recommend PHP, but I think it has way too bad a name because of its past.
[ambrus]: This is different from MS Word, which was already a good editor in the pre-unicode days (in word for windows versions 2 and 6, which ran on windows 3 but also on windows 95), only it wasn't trying to solve the task of writing maths papers back then.
[Discipulus]: ah ok, sounds reasonable; with no fear: Perl all life long
[ambrus]: Mind you, LaTeX is currently still useful for writing math paper or snippet content without styling in such a way that the
[ambrus]: formatting conventions of a journal or website can be quickly applied to it, and MS Office and LibreOffice has not quite solved this (although it's better for this than it used to be),
[ambrus]: which is sort of a drawback compared to the ages of typewritten manuscripts representing content only to which the typesetter applies formatting, but that process required much more manual labor.
[ambrus]: If you want to typeset a manuscript, you can still do much less work then in the manual typesetting ages and get good formatting.
[ambrus]: All with only cheap modern computers and software.
[ambrus]: Something you can have at home and your corner print shop, without a whole printing press's worth of equipment.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2017-09-26 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (293 votes). Check out past polls.

    Notices?