Thanks folks for your help! I am sorry for not summarizing earlier but I had to do some more work, both with your suggestions and do some more research. I could summarize as follows: 1) I need to swap keys because the natural order of processing is sometimes over one key first and sometimes over the other first. It is part of refactoring legacy code. I am looking at modifying the using code so one order is enough, but first thing first. 2) A typical 2D hash is about a thousand records large (major X minor key sizes) and we process a couple of thousands hashes in a single run. Yes, performance matters! 3) Yes, the calling code does "know" the required key priority, and I am providing the user with two retrieving subs, one with a swapped key order from the first, using an internal swapping code which is essentially what I posted. 3) I often use a "flattened", composite hash key, especially when I would otherwise need to do a many-levels deep sorting. But it has it an more overhead then swapping, concatenating and then splitting keys back. So see item (2) above. 4) My understanding is that 'foreach' is just an alias for 'each'. Am I wrong?
Regards Meir -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Shlomi Fish Sent: שבת 20 יולי 2013 18:33 To: Perl in Israel Subject: Re: [Israel.pm] Swapping the key order of a 2D hash Hi Meir, On Sat, 20 Jul 2013 15:33:06 +0300 Meir Guttman <[email protected]> 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 > [email protected] > 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 [email protected] http://mail.perl.org.il/mailman/listinfo/perl ----- No virus found in this message. Checked by AVG - www.avg.com Version: 2013.0.3349 / Virus Database: 3204/6506 - Release Date: 07/20/13 _______________________________________________ Perl mailing list [email protected] http://mail.perl.org.il/mailman/listinfo/perl
