Shlomi, you manage to teach me a new thing every time… Thanks!

Gaal, hmmm, it is actually 100-200 x 10. I’ll definitely try your approach…! 
Thank you too!

Thanks folk!

Meir

 

From: [email protected] [mailto:[email protected]] On Behalf Of 
Gaal Yahas
Sent: יום ג 23 יולי 2013 21:08
To: Perl in Israel
Subject: Re: [Israel.pm] Swapping the key order of a 2D hash

 

Meir: well, nested structures have overhead, and hashes in particular have cost 
that adds up. Sounds like you have 30 keys of each kind. Are these more or less 
constant? Consider mapping every key to a numerical value, and using one flat 
array for storing your actual data. Lookup is then $data[ $majors{$major} * 
$max_major + $minors{$minor} ]. Less convenient, sure, but it sounds like you 
need more speed. If your keys really are constants you should use constant 
MAJOR_TOM => 16 etc. and then these expressions are slightly tidier and run 
faster yet.

 

 

On Tue, Jul 23, 2013 at 7:50 PM, Shlomi Fish <[email protected]> wrote:

Hi Meir,


On Tue, 23 Jul 2013 19:03:59 +0300
Meir Guttman <[email protected]> wrote:

> Thanks folks for your help!

> 4) My understanding is that 'foreach' is just an alias for
> 'each'. Am I wrong?
>

No, “foreach” and “for” are aliases. “each()” is different and should be used
with while:

[SHELL]

shlomif@telaviv1:~$ cat each-example.pl
#!/usr/bin/perl

use strict;
use warnings;

my %people =
(
    'George' => 'Washington',
    'James' => 'Cooke',
    'Benjamin' => 'Franklin',
);

while (my ($key, $value) = each(%people))
{
    print "Key=$key Value=$value\n";
}
shlomif@telaviv1:~$ perl each-example.pl
Key=James Value=Cooke
Key=Benjamin Value=Franklin
Key=George Value=Washington
shlomif@telaviv1:~$

[/SHELL]

On more recent versions of perl, each can also be used on arrays to retrieve
the index and value repeatedly.

Regards,

        Shlomi Fish


> 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


--
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/

List of Portability Libraries - http://shlom.in/port-libs

But if you’re writing [open source software] for the world, you have to listen
to your customers — this doesn’t change just because they’re not paying you in
money.
    — Eric S. Raymond in The Cathedral and the Bazaar


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





 

-- 
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