Hi Anonymous,

Others have already pointed out you can just edit the hashes through the references directly, and that seems like the way to go in your case. Just to explain why your second two examples don't work: Arrays or hashes on the left hand side of a my (...) = ... assignment only make sense as the last item being assigned to, because they slurp up all the remaining values from the right hand side.

That means when you write my (%x, %y, %z) = ..., then the hash %x will slurp up all the values assigned to it:

$ perl -wMstrict -MData::Dumper -le 'my (%x,%y) = (1,2,3,4); print Dum +per(\%x,\%y)' $VAR1 = { '1' => 2, '3' => 4 }; $VAR2 = {};

Also, be aware that my %copy = %hash; only makes a shallow copy, so if %hash contains values which are references to other data, then those will reference the same data structures - which may not be what you want. If you want deep copies, Storable's dclone or Clone may be able to help you there.

And just for completeness, here's one ("elegant"?) way to make the shallow copies:

use warnings; use strict; use Data::Dump 'pp'; my %x = ("a" => "red"); my %y = ("b" => "green"); my %z = ("c" => "black"); pp \%x, \%y, \%z; my ($r1, $r2, $r3) = modfifyHash(\%x, \%y, \%z); pp $r1, $r2, $r3; pp \%x, \%y, \%z; # these remain unmodified sub modfifyHash { #my ($x, $y, $z) = @_; # this would modify the original hashes my ($x, $y, $z) = map { { %{ $_ } } } @_; # ^ ^ ^ # | | for each argument # | dereference as hash # make new hash reference (shallow copy!) $x->{a} = "circle"; $y->{b} = "square"; $z->{c} = "rectangle"; return $x, $y, $z; } __END__ ({ a => "red" }, { b => "green" }, { c => "black" }) ({ a => "circle" }, { b => "square" }, { c => "rectangle" }) ({ a => "red" }, { b => "green" }, { c => "black" })

Although I would second Eily's suggestion that it might be a good idea to combine the hashes into one.

Hope this helps,
-- Hauke D


In reply to Re: Elegantly dereferencing multiple references by haukex
in thread Elegantly dereferencing multiple references by Anonymous Monk

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.