in reply to Two simple code style advice questions

This is probably an over optimization, but for really long lists, I use:

my %ntests = map { $_ => undef } @tests;

And then test using exists. It reduces the memory footprint slightly (or at least, it used to ... I admit I haven't verified that it's still true in more recent versions of perl.)

Replies are listed 'Best First'.
Re^2: Two simple code style advice questions
by choroba (Cardinal) on Jan 16, 2013 at 16:16 UTC
    Probably not true anymore (v5.10.1):
    $ perl -E 'use Devel::Size qw(total_size); my (%h1, %h2); my @ar = 0 .. 1e6; undef @h1{@ar}; %h2 = map { $_ => undef } @ar; say for map total_size($_), \%h1, \%h2; ' 60083287 60083287
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: Two simple code style advice questions
by parv (Parson) on Jan 16, 2013 at 16:02 UTC

    In that case I am highly partial to...

    my %p; @p{ @q } = ();
      Or even
      my %p; undef @p{ @q } if @q;
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: Two simple code style advice questions
by Anonymous Monk on Jan 16, 2013 at 16:02 UTC
    Not sure the point of this. If you are willing to test using exists, then why bother initializing at all? $hash{key}++ is a perfectly valid way to bring a key into existence.