Hello,

My code is sending a hash by reference to a function and adds an element to it. The problem is that the new element isn't in the hash that I sent.

my %hash = ( 'animal' => 'bear', 'plant' => 'oak', 'fungus' => 'truffle', ); test(\%hash); print join(",", keys (%hash)),"\n"; sub test { my ($ref) = @_; my %ref_hash = %{$ref}; $ref_hash{'new'} = 'new_v'; print join(",", keys (%ref_hash)),"\n"; }

Output:

plant,new,fungus,animal + + plant,animal,fungus

I want the 'new' element to be also in the new one. I think I didn't quit understand how references work.

Output I want:

plant,new,fungus,animal + + plant,new,fungus,animal

2018-03-22 Athanasius Added code and paragraph tags


In reply to passing hash ref to a function by ovedpo15

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.