First off, ++ for asking the question nicely and for including use strict; use warnings; in your code. Not so good however is that there is a syntax error in the code that you posted. \$A{$_} .= "hello"; #Change Values is broken syntax and should have generated an error when you tied to run the code. Mentioning the error would leave less egg on your face!

That aside, you have vaugely the right idea, you just haven't found the right way to apply it. Try this instead:

use strict; use warnings; my %hash=("bob" =>123, "tom" => "CAT"); #Assign values change (\%hash); #Print New Values print "$_ = $hash{$_}\n" foreach keys %hash; sub change{ my $hashRef = shift; #Get the reference to the values foreach (keys %$hashRef){ print "$_ = $hashRef->{$_}\n"; $hashRef->{$_} .= "hello"; #Change Values } }

Prints:

tom = CAT bob = 123 tom = CAThello bob = 123hello

Note in particular that a reference to the hash is passed to the sub and that the reference has to be dereferenced with -> to access the hash.


DWIM is Perl's answer to Gödel

In reply to Re: referances of hashes, and subroutines by GrandFather
in thread referances of hashes, and subroutines by tempest

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.