You are not using true multidimensional hashes. Perl doesn't have true multidimensional hashes. However, it offers two ways to emulate multidimensional structures.

One way is to use references; this is the way you're currently doing it. A reference is like a link or a pointer. When you copy the outer structure, you're just copying a collection of links to the inner structures. The new copy is still pointing at the same inner structures, so altering the inner structures will affect both outer structures.

Perl does offer another mechanism for emulating multi-dimensional hashes. It is barely documented (mentioned in perlvar) and rarely used, but actually works quite well in some situations.

use strict; use warnings; use Data::Dumper; local $; = '#'; my %hash1; $hash1{'Hello', 'World'} = 1; $hash1{'Hello', 'Pluto'} = 2; $hash1{'Goodbye', 'World'} = 3; $hash1{'Goodbye', 'Pluto'} = 4; my %hash2 = %hash1; # copy $hash2{'Goodbye', 'Pluto'}++; # alter hash 2 print "$hash2{'Goodbye', 'Pluto'}\n"; # show that it is altered print "$hash1{'Goodbye', 'Pluto'}\n"; # hash1 remains unaltered # Let's see what's going on here... print Dumper(\%hash1, \%hash2);
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: Copy of multidimensional hash by tobyink
in thread Copy of multidimensional hash by tommaso.fornaciari

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!
  • 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, details, 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, summary, 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.