The problem is the hash copy because it's a shallow copy. %hash1 and %hash2 have a key by the name of 'Quantity' which happen to point to the same thing...namely {x=>1,x=>2}. In fact the fidleHash() sub doesn't change the hash which comes in as an argument, rather it changes the hash which the 'Quantity' key is pointing to. Insert the following two lines after copying the hash: my %hashCopy = %{$hash1{'Quantity'}} ; $hash2{'Quantity'} = \%hashCopy ; ...and you won't see the problem anymore. That way you also create a copy of what 'Quantity' points to.

In reply to Re: changing copy of a hash changes original by Anonymous Monk
in thread changing copy of a hash changes original by edo

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.