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

Re: Copy of multidimensional hash

by tobyink (Canon)
on Jan 03, 2013 at 09:42 UTC ( [id://1011427]=note: print w/replies, xml ) Need Help??


in reply to Copy of multidimensional hash

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'

Replies are listed 'Best First'.
Re^2: Copy of multidimensional hash
by tommaso.fornaciari (Initiate) on Jan 03, 2013 at 10:06 UTC

    Dear Tobyink,
    thanks to you too for the nice tip :))

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found