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

How can I re-initialize the hash values. I have used the following hash,
my %hash=('a'=>0 , 'e'=>0 , 'i'=>0 , 'o'=>0 , 'u'=> 0,); ... some code with hash values..... %hash=('a'=>0 , 'e'=>0 , 'i'=>0 , 'o'=>0 , 'u'=> 0,); # + Re-initialize the hash values.
I want some other way to re-initialize the hash values with out using above and foreach iteration way. Thanks in advance...

Replies are listed 'Best First'.
Re: re-initialize the hash
by Corion (Patriarch) on Apr 01, 2010 at 08:16 UTC

    Maybe store your defaults separately:

    my %hash_defaults = ('a'=>0 , 'e'=>0 , 'i'=>0 , 'o'=>0 , 'u'=> 0,); my %hash = %hash_defaults; ... # Reinitialize %hash = %hash_defaults;

    ... or restructure your loop code so that the initialization and reinitialization use the same code:

    while (...) { my %hash = ('a'=>0 , 'e'=>0 , 'i'=>0 , 'o'=>0 , 'u'=> 0,); ... };

    ... or use local (but that only works for global variables, not lexical variables):

    use vars '%hash'; %hash = ('a'=>0 , 'e'=>0 , 'i'=>0 , 'o'=>0 , 'u'=> 0,); ... do { local %hash = %hash; ... make some changes }; ... changes are forgotten
      Hi Monks, I found the following way. Just copy the array to values of the hash which array contains the initialized values.Is it correct?
      use strict; use warnings; use Data::Dumper; my @array=(0,0,0); my %hash=('a'=>1 , 'b' =>2 , 'c'=>3,); my @values=keys%hash; @hash{@values}=@array; print Dumper \%hash;
        Yes, but unneccessarily complicated. Since you can assign a hash to an array and it will do the sensible thing, your code can be shortened to

        use strict; use warnings; use Data::Dumper; my @array=('a',0,'b',0,'c',0); my %hash=('a'=>1 , 'b' =>2 , 'c'=>3,); %hash=@array; print Dumper \%hash;

        Your version has the advantage that if some of the keys should change, you still would reset them to 0. If you want to reset all values to 0 you don't need the array anymore:

        @hash{keys %hash}=(0) x scalar(keys %hash);

        CORRECTION:Forgot () around the 0. And as cdarke pointed out to me the scalar is not needed as it is already in scalar context. But I tend to play safe with these things instead of trusting my memory

Re: re-initialize the hash
by jethro (Monsignor) on Apr 01, 2010 at 08:46 UTC
    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

      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' };