Although you have already been given detailed answers, just one quick question to let you think about it: if you passed an array to your sub, you would not expect it to modify the array subscripts, would you?

Admittedly, it is not quite exactly the same, but you don't need a sub to figure out that you simply cannot modify the keys of a hash (other than by deleting an existing entry and creating another one.

What is happening in your sub is that the values associated with the hash keys are actually modified in @_, but this has no effect on the hash itself, whereas the values associated with the hash values are actually modified within the hash, as it can be seen with this slight modification of your one-liner:

$ perl -MData::Dumper -we 'sub t{ $_+=1 for @_; print Dumper \@_;} my + %h = (1 => 2, 3 => 4); t(%h); print Dumper \%h' $VAR1 = [ 2, 3, 4, 5 ]; $VAR1 = { '1' => 3, '3' => 5 };

In reply to Re: Modifying hash keys via aliasing by Laurent_R
in thread Modifying hash keys via aliasing by vsespb

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.