Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Two simple code style advice questions

by jhourcle (Prior)
on Jan 16, 2013 at 15:54 UTC ( [id://1013596]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1013596]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-18 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found