in reply to re-initialize the hash

Or use a subroutine to deliver the values:

my %hash= initvalues(); ... %hash= initvalues(); ... sub initvalues { return ('a'=>0, ...); }

or with sideffects

my %hash; inithash(\%hash); .... inithash(\%hash); ... sub inithash { my $h= shift; $_[0]= { 'a'=>0, ... }; #note the curled braces #CORRECTED %$h= ( 'a'=>0, ... ); #alternative to the previous line }

UPDATE: First alternative in the inithash sub didn't work because it was changing the pointer, now it should work

Replies are listed 'Best First'.
Re^2: re-initialize the hash
by AnomalousMonk (Archbishop) on Apr 01, 2010 at 09:07 UTC
    sub inithash { my $h= shift; $h= { 'a'=>0, ... }; #note the curled braces %$h= ( 'a'=>0, ... ); #alternative to the previous line }

    The first of these methods, i.e.
        $h= { 'a'=>0, ... };
    does not work:

    >perl -wMstrict -le "use Data::Dumper; my %hash = qw(a 1 b 2 c 3); print Dumper \%hash; init(\%hash); print Dumper \%hash; sub init { my $h = shift; $h = { foo => 'bar' }; } " $VAR1 = { 'c' => '3', 'a' => '1', 'b' => '2' }; $VAR1 = { 'c' => '3', 'a' => '1', 'b' => '2' };