in reply to Re: Getting a branch of a nested hash
in thread Getting a branch of a nested hash
Note: my %page = %{ ... }; is making a copy of the hash. Changes will not be retained.Are you sure? Try running this:
A hash copy will only do a shallow copy, i.e. the references are copied instead of the contents of those references. That's why changes to %hash2 also end up in %hash. Using Devel::Peek to display %hash and %hash2 will make this even more clear. Search for the lines looking like:#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = (key1 => { subkey1 => [ qw(a number of elements in an array) + ], }, key2 => "value2"); my %hash2 = %hash; $hash2{key1}->{subkey1} = [ qw(other elements in an array) ]; print Dumper(\%hash); print Dumper(\%hash2);
Elt "key2" HASH = 0x3e4d4a SV = PV(0x80f6498) at 0x81009c4
and note the addresses.
Arjen
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re3: Getting a branch of a nested hash
by dragonchild (Archbishop) on Jan 27, 2003 at 22:24 UTC |