Hi Meir,

On Sat, 20 Jul 2013 15:33:06 +0300
Meir Guttman <m...@guttman.co.il> 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?

I can't think of a good and efficient way to do it using 'map' alone, because
you swap the order of the nestings. You can write a subroutine that accepts two
hash references and does the swapping for you like you demonstrated.

BTW, you can save a little by doing «my $orig_major_val =
$original{$major_key}» or perhaps by using each() instead of «foreach ..
keys» (though I was told each() is not always receommended.

Regards,

        Shlomi Fish

> Or is there a utility for that?
> I do it a lot, and performance matters...
> Meir
> 
> _______________________________________________
> Perl mailing list
> Perl@perl.org.il
> http://mail.perl.org.il/mailman/listinfo/perl



-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/

Katie: in case you have any interest in me, I should note that I have a policy
against getting involved with people who are 4 times my senior or more.
    — http://www.shlomifish.org/humour/Star-Trek/We-the-Living-Dead/

Please reply to list if it's a mailing list post - http://shlom.in/reply .
_______________________________________________
Perl mailing list
Perl@perl.org.il
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to