in reply to Re: Hash multiple initialization
in thread Hash multiple initialization

Might this be a case in which prototyping is actually useful as a syntactic sweetener?

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; }
Output:
$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
    I don't see the point of the prototype. It actually makes your example caller bigger!
    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; }