Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I just realized the question is useless.

I'm not sure why you think so, how to take a slice of a hash is a valid question, maybe you could explain what resolution you found for your question?

Anyway, there are several ways to make a (shallow) copy of a part of a hash, here are a few:

  1. Using map: %slice = map { $_ => $hash{$_} } @keys;
  2. Using Foreach Loops (in this case as a statement modifier): $slice{$_} = $hash{$_} for @keys;
  3. Using Slices: @slice{@keys} = @hash{@keys};
  4. Using Key/Value Hash Slices, new in Perl 5.20: %slice = %hash{@keys};
my %h is really big and I don't have enough RAM to make a copy of it

This makes the question interesting. It turns out that, at least on my machine with Perl 5.26, from the options above:

  1. map takes the most memory, about 180% as much as a regular @ slice,
  2. a regular @ slice takes about as much memory as a key/value % slice,
  3. a key/value % slice takes about as much memory as a regular @ slice, and
  4. the for loop variant takes the least amount of memory, taking only about 88% of a regular @ slice.
use warnings; use strict; use feature qw/say/; my %bighash; { my $sz = 1_000_000; my $v = 'aaaaaaaaaaaaaa'; $bighash{$v++} = int(rand($sz))+1 for 1..$sz; } my @keys = keys %bighash; delete $bighash{aaaaaaaaaaaaab}; my %slice; say `ps -orss $$`=~s/\s+/ /gr; #%slice = map { $_ => $bighash{$_} } @keys; #@slice{@keys} = @bighash{@keys}; #%slice = %bighash{@keys}; $slice{$_} = $bighash{$_} for @keys; die if defined $slice{aaaaaaaaaaaaab}; # verify the slice die unless $slice{aaaaaaaaaaaaaa}>0 && $slice{aaaaaaaaaaaaaa}==$bighash{aaaaaaaaaaaaaa}; say `ps -orss $$`=~s/\s+/ /gr;
I want to load the slice directly into a hashref

As it turns out, changing my %slice into my $slice and changing the syntaxes into the following doesn't really make a big difference:

  1. $slice = { map { $_ => $hash{$_} } @keys }; - about 6% more memory
  2. @{$slice}{@keys} = @hash{@keys}; - about the same amount of memory
  3. $slice = {%hash{@keys}}; - about the same amount of memory
  4. $slice->{$_} = $hash{$_} for @keys; - about the same amount of memory

On the other hand, if for example the values in your hash are huge, then perhaps a different approach is needed - for example, in your second hash you could store references to the values of the first hash. Or, you may not even need to create a second hash in the first place, perhaps simply keeping the list of keys around to select values from the first hash is enough.

In reply to Re: Hash slice from a hashref, into a hashref by haukex
in thread This is a useless node by shagbark

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (4)
    As of 2020-01-25 02:59 GMT
    Find Nodes?
      Voting Booth?