in reply to Help me improve this sub with named params!
This code is careful not to add additional key=>undef pairs to the hash, thus the exists check.my @valid = qw/buttons action foo bar xyz/; my %args = ( buttons => [], action => 'parse.cgi', @_ ); %args = map { exists $args{$_} ? $_ => $args{$_} : () } @valid; while (my ($k,$v) = each %args) { ## etc... }
Alternately, you could just iterate over all valid keys and only print the ones that are available in the hash (instead of using each). This method would always print the list in a predictable order, if that's important to you:
my @valid = qw/buttons action foo bar xyz/; my %args = ( buttons => [], action => 'parse.cgi', @_ ); foreach my $k (@valid) { next if not exists $args{$k}; my $v = $args{$k}; $v = @$v if ref $v eq 'ARRAY'; print "$k => $v\n"; }
blokhead
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Help me improve this sub with named params!
by BUU (Prior) on Aug 30, 2003 at 04:13 UTC | |
by blokhead (Monsignor) on Aug 30, 2003 at 05:36 UTC |