Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
why is it a "horrible" hash concatenation trick?

It's just that it does way too much unnecessary operations.  To "visualize" what's going on, you could tie the hash and put debugging prints into the routines like STORE, FETCH, etc.  For example:

#!/usr/bin/perl -l package MyHash; use Tie::Hash; our @ISA = 'Tie::StdHash'; sub TIEHASH { my $class = shift; return bless {}, $class; } sub STORE { my $self = shift; print "storing $_[0] => $_[1]"; $self->SUPER::STORE(@_); } sub FETCH { my $self = shift; print "fetching @_"; $self->SUPER::FETCH(@_); } sub CLEAR { my $self = shift; print "clear"; $self->SUPER::CLEAR(); } package main; use strict; use warnings; use Data::Dumper; tie my %myhash, "MyHash"; print "Variant 1 (bad):"; %myhash = ( %myhash, "key_$_" => { foo => 42 } ) for 1..5; print "\nDumping..."; print Dumper \%myhash; %myhash = (); print "\nVariant 2 (good):"; $myhash{"key_$_"} = { foo => 42 } for 1..5; print "\nDumping..."; print Dumper \%myhash; __END__ Variant 1 (bad): clear storing key_1 => HASH(0x65ecb0) fetching key_1 clear storing key_1 => HASH(0x65ecb0) storing key_2 => HASH(0x6c2050) fetching key_1 fetching key_2 clear storing key_1 => HASH(0x65ecb0) storing key_2 => HASH(0x6c2050) storing key_3 => HASH(0x74a420) fetching key_3 fetching key_1 fetching key_2 clear storing key_3 => HASH(0x74a420) storing key_1 => HASH(0x65ecb0) storing key_2 => HASH(0x6c2050) storing key_4 => HASH(0x74a3f0) fetching key_3 fetching key_1 fetching key_4 fetching key_2 clear storing key_3 => HASH(0x74a420) storing key_1 => HASH(0x65ecb0) storing key_4 => HASH(0x74a3f0) storing key_2 => HASH(0x6c2050) storing key_5 => HASH(0x74aaf0) Dumping... fetching key_3 fetching key_1 fetching key_5 fetching key_2 fetching key_4 $VAR1 = { 'key_3' => { 'foo' => 42 }, 'key_1' => { 'foo' => 42 }, 'key_5' => { 'foo' => 42 }, 'key_2' => { 'foo' => 42 }, 'key_4' => { 'foo' => 42 } }; clear Variant 2 (good): storing key_1 => HASH(0x74ac80) storing key_2 => HASH(0x737c40) storing key_3 => HASH(0x6c2050) storing key_4 => HASH(0x74ad00) storing key_5 => HASH(0x74ac00) Dumping... fetching key_3 fetching key_1 fetching key_5 fetching key_4 fetching key_2 $VAR1 = { 'key_3' => { 'foo' => 42 }, 'key_1' => { 'foo' => 42 }, 'key_5' => { 'foo' => 42 }, 'key_4' => { 'foo' => 42 }, 'key_2' => { 'foo' => 42 } };

In reply to Re^3: Getting loopy while appending HoH by almut
in thread Getting loopy while appending HoH by jedikaiti

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (4)
    As of 2014-07-23 02:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (131 votes), past polls