Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello

sorry I'm sort of a newbie and just started learning perl about 2 weeks ago.

I was wondering if a hash value already exists, can you append data to it?

for example if $hash{'key_one'} = "my value"; Then can I append to it? I tried this code but it gave me a syntax error:
$hash{'key_one'} =. "my value 2";

Thanks

Replies are listed 'Best First'.
Re: Appending value to a hash value
by Zaxo (Archbishop) on May 05, 2005 at 03:52 UTC

    The only problem is a typo:

    $hash{'key_one'} .= "my value 2"; # ^^ =. is not an operator

    After Compline,
    Zaxo

      Yes, all modify-assign operators use the operator it's based upon, on the left hand side of the "=". See Assignment Operators in perlop.
Re: Appending value to a hash value
by Eimi Metamorphoumai (Deacon) on May 05, 2005 at 03:54 UTC
    If you use
    $hash{'key_one'} .= "my value 2";
    then you'll end up with $hash{'key_one'}=my valuemy value 2". If that's what you want, then that's great. If you want to have both values separate, you may need to use a hash of arrays (perldoc perlreftut for more information).