develooper Front page | perl.beginners | Postings from August 2009

Re: Inverting a hash safely

Thread Previous | Thread Next
Shawn H. Corey
August 3, 2009 17:50
Re: Inverting a hash safely
Message ID:
Jenda Krynicky wrote:
> From: "Shawn H. Corey" <>
>> push @{ $r{$h{$_}} }, $_ for keys %h;   # one line :)
> Is it simple enough so that you immediately know what does it do? I 
> guess not. IMHO it's complex enough to warrant being moved to a named 
> subroutine. Especially since it forces you to write the name of the 
> original hash twice.

Now, that's not a fair question.  Of course I know what it does; I wrote it.

But then again I never have to invert a hash; when I populate it, I 
would populate its inverse as well.  I would build both data structures 
at the same time, inserting only the data I need, where I need it.

And no, this would not take more room since you have to have both in 
memory simultaneously to invert them.

It is faster since you don't have to scan the same data again to invert 

Which is why questions like this always seem to be a kludge to me. 
Someone's manager sees no need to do it right.  The problem is kludges 
are like cancer; they have uncontrolled growth.

Just my 0.00000002 million dollars worth,

Programming is as much about organization and communication
as it is about coding.

My favourite four-letter word is "Done!"

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About