agoth has asked for the wisdom of the Perl Monks concerning the following question:
I want to replace myfunc below with a short but legible bit of code, the last attempt below ends up using $_, @_ and %_ which is a bit mean.
Any better offers??
my @usual = qw ( data email stuff ); #--- fixed length my $i = 0; my %hash; my @input = ( #--- rather have no function { username => 'fred1', myfunc ( 12, 56, 78 ) }, #--- no use if have to reset $i { username => 'fnerk', map { $usual[ $i++ ] => $_ } (33, 44, 55) } +, #--- hmm { username => 'aargh', map { @hash{@usual} = @$_; %hash } [ 33, 44 +, 55 ] }, #--- longwinded { username => 'fnerk', map { $tmp = shift @usual; push @usual,$tmp; $tmp, $_ } (33, 44, 55) }, #--- heinous { username => 'aargh', map { @_{ @usual } = @$_; %_ } [ 33, 44, 55 + ] }, ); sub myfunc { my %flib; @flib{ @usual } = @_; return %flib; } for ( @input ) { my %local = %$_; for (sort keys %local ) { print "key : $_ : val : $local{$_} : "; } print "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: mapping two arrays into anon hash
by Masem (Monsignor) on Jan 08, 2002 at 17:07 UTC | |
by agoth (Chaplain) on Jan 08, 2002 at 17:12 UTC | |
by Masem (Monsignor) on Jan 08, 2002 at 17:49 UTC | |
by agoth (Chaplain) on Jan 08, 2002 at 18:02 UTC | |
|
Re (tilly) 1: mapping two arrays into anon hash
by tilly (Archbishop) on Jan 08, 2002 at 19:24 UTC | |
|
Re: mapping two arrays into anon hash
by jmcnamara (Monsignor) on Jan 08, 2002 at 17:22 UTC |