On Thu, Mar 6, 2008 at 10:50 AM, yitzle <[EMAIL PROTECTED]> wrote:
> I don't know much about how Perl deals with this stuff, but what
>  you've done is made a copy of the pointer/reference.
>  Both variables are referencing the same memory/hash.
>  What you want to do is copy the hash, not copy the reference to it.
>  I /think/ this ought to work:
>  my %hash = %{$fileSize};
>
>  __CODE__
>  my $h1 = {a=>1,b=>2};
>  print join(", ", keys %{$h1}), "\n";
>  my $h2 = $h1; # References same object
>  $$h2{c} = 3;
>  print join(", ", keys %{$h1}), "\n";
>  %copy = %{$h1}; # Copy to a new has
>  $copy{d} = 4; # Doesn't affect the original object
>  print join(", ", keys %{$h1}), "\n";
>  print join(", ", keys %copy), "\n";
>  __OUTPUT__
>  a, b
>  c, a, b
>  c, a, b
>  c, a, b, d
>  __END__
>
>  You can then reference the new copy if you so desire.
>  my $refToCopy = \%copy;

As Chas points out, you need something more ___ than this. My code
would simply produce a "shallow copy" where the hash's elements are
still referencing the same final location. If this was a simple hash
that did not contain references, it would suffice. However, for a HoH,
the children hashed also need to be copied ("deep copy") which my
solution does not do.
Thanks, Chas!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to