in reply to How to process named parameters that have the same key name?
printsuse Data::Dumper; use strict; use warnings; simple_call( animal => 'monkey', fish => 'tuna', insect => 'ant', insect => 'spider' ); ########################## sub simple_call { my %params; while(my($key, $value) = splice @_, 0, 2) { if (exists $params{$key}){ if (ref $params{$key} eq "ARRAY"){ push @{$params{$key}},$value; }else{ $params{$key} = [$params{$key},$value]; } }else{ $params{$key} = $value; } } print Dumper \%params; }
Update:Corrected attribution to saintmike, and deleted unnecessary @params declaration.$VAR1 = { 'insect' => [ 'ant', 'spider' ], 'animal' => 'monkey', 'fish' => 'tuna' };
"Man cannot live by bread alone...
He'd better have some goat cheese and wine to go with it!"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to process named parameters that have the same key name?
by maard (Pilgrim) on Oct 19, 2005 at 10:58 UTC |