How large is your data?

I agree with Omer. Also, do you really need a nested structure at all? You
could tuple key1, key2 in a flat hash(*). Depending on what lookups you
need to make that might be acceptable.

As usual with anything having to do with performance, you should write a
benchmark and try out several approaches. It's not a lot more than the code
you already wrote in your email.


(*) Perl hash table keys are strings. The simplest way to do this is if you
know a character that is never your data and join the keys on that. There's
actually a little-known syntactic feature that does this for you: $h{$key1,
$key2} joins on $;. Of course, once you do this you can't (cheaply) look up
only the elements that have $key1. That may or may not be too expensive.



On Sat, Jul 20, 2013 at 5:15 PM, Omer Zak <[email protected]> wrote:

> On Sat, 2013-07-20 at 15:33 +0300, Meir Guttman wrote:
> > Hello Perlers,
> > Sometimes I would like to swap the order of the two keys of a
> > TWO-DIMENTIONAL hash. I could of course do it in a straight forward way
> as:
> > my %original;
> > my %swapped;
> > foreach my $major_key (keys %original){
> >     foreach my $minor_key (keys %original{$major_key}){
> >       $swapped{$minor_key}{$major_key} =
> $original{$major_key}{$minor_key};
> >     }
> > }
> > Simple enough!
> > I wonder though how can I use 'map' in such a case, or can I?
> > Or is there a utility for that?
> > I do it a lot, and performance matters...
>
> Why do you need to swap keys at all?
> If the hash is big and accesses do not need to be optimized for speed,
> then I would access the hash via a function which accepts a flag telling
> it to swap/not to swap the keys.
>
> Or maybe the calling code has a way to figure out when to swap the keys
> before accessing the hash?
>
> --- Omer
>
>
> --
> Every good master plan involves building a time machine.  Moshe Zadka
> My own blog is at http://www.zak.co.il/tddpirate/
>
> My opinions, as expressed in this E-mail message, are mine alone.
> They do not represent the official policy of any organization with which
> I may be affiliated in any way.
> WARNING TO SPAMMERS:  at http://www.zak.co.il/spamwarning.html
>
> _______________________________________________
> Perl mailing list
> [email protected]
> http://mail.perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <[email protected]>
http://gaal.livejournal.com/
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to