user30771 user30771 - 8 months ago 58
Perl Question

Concatenate hash value when hash is referenced

This is my code:

my $hash = shift; // in this hash i have a key 'key' that has the value 'this is a'

$hash{'key'} .= 'string'; //trying to concatenate the two strings

use Data::Dumper;

print Dumper $hash{'key'}; // prints "hash(0x36fc12..) string"

I am trying to get :

key=>'this is a string'


Always use the following!!!

use strict;
use warnings qw( all );

It would have pointed out your error.

You modify the hash %hash, but there is no such hash! You want to modify the hash referenced by $hash, so you want

use Data::Dumper qw( Dumper );

sub f {
   my $hash = shift;
   $hash->{key} .= ' string';

my %hash = ( key => 'this is a' );