Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Idiom: hashes as sets

by perrin (Chancellor)
on Jul 02, 2008 at 18:39 UTC ( #695177=note: print w/ replies, xml ) Need Help??


in reply to Idiom: hashes as sets

One tweak to save memory: use undef instead of 1 for the value and use exists to check for an item in the set. It makes things harder to read though.


Comment on Re: Idiom: hashes as sets
Re^2: Idiom: hashes as sets
by Narveson (Chaplain) on Jul 02, 2008 at 19:22 UTC

    I agree, and note what this means for inserting a list:

    my %set; @set{qw(I agree)} = (); print sort keys %set; # Iagree

    Just didn't want you to say

    @set{@list} = (undef) x scalar @list;

    even though that would work.

      I don't understand the reply. Could you, Narveson, please rephrase?
Re^2: Idiom: hashes as sets
by Anonymous Monk on Jul 03, 2008 at 14:22 UTC
    use !undef and get the best of both worlds :)(tye learn me this)
      Whoa, 1 and undef are on par , optimization?
      #!/usr/bin/perl -- use Devel::Size qw(size total_size); use strict; use warnings; { my %h; for ( 1 .. 20_000 ){ $h{1}{$_}=1; $h{'$_'}{$_}=$_; $h{'undef'}{$_}=undef; $h{'!undef'}{$_}= !undef; $h{'!!undef'}{$_}= !!undef; $h{'!!!undef'}{$_}= !!!undef; $h{'sv_yes'}{$_}= !0; $h{'sv_no'}{$_}= !1; } printf "%10s %10s %10s\n", 'value', 'size', 'total_size'; printf "%10s %10d %10d\n",$_, size($h{$_}),total_size($h{$_}) for +sort keys %h; }
      ActivePerl Build 813, perl, v5.8.7 built for MSWin32-x86-multi-thread
      value size total_size !!!undef 660026 1420026 !!undef 660026 1400026 !undef 660026 1420026 $_ 660026 1328920 1 660026 980026 sv_no 660026 1400026 sv_yes 660026 1420026 undef 660026 900026
      strawberry-perl-5.10.0.1-1.exe , v5.10.0 built for MSWin32-x86-multi-thread
      value size total_size !!!undef 660014 1540014 !!undef 660014 1540014 !undef 660014 1540014 $_ 660014 1616018 1 660014 980014 sv_no 660014 1540014 sv_yes 660014 1540014 undef 660014 980014

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (15)
As of 2014-04-16 17:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls