in reply to Re: Hash multiple initialization
in thread Hash multiple initialization
Output:sub mass_init (\%\@$); my @init_to_toto = qw{ a b c f t u }; my %hash = ( y => 'pre', z => 'pre', ); mass_init(%hash, @init_to_toto, 'toto'); mass_init(%hash, @{[ qw(b g k p) ]}, 'TITI'); use Data::Dumper; print Dumper \%hash; sub mass_init (\%\@$) { my ($hashref, $arrayref, $string) = @_; # $hashref->{$_} = $string for @$arrayref; @{ %$hashref }{ @$arrayref } = ($string) x @$arrayref; }
$VAR1 = { 'a' => 'toto', 'y' => 'pre', 'p' => 'TITI', 'c' => 'toto', 'u' => 'toto', 'k' => 'TITI', 'g' => 'TITI', 'b' => 'TITI', 'z' => 'pre', 'f' => 'toto', 't' => 'toto' };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Hash multiple initialization
by ikegami (Patriarch) on Oct 09, 2008 at 01:13 UTC |