in reply to Multiple Data Selections per Field in CGI
So you are only getting the last element, eh? This leads me to believe that you are using something like this:
That doesn't work when you have a query string likesplit up query string and decode pairs into $val and $key insert $key into params hash with value $val
because foo will be equal to 'tofu' - what you need to do is first check if the key exists, if it does then treat it like an array.foo.cgi?foo=mashed&foo=fried&foo=boiled&foo=tofu
But there's a catch - if the key exists, then is the value a scalar, or is it an array? If it's the first time the key is encountered, then the value is simply a scalar. The second time the key is encountered, it's still a scalar. The value needs to be converted to an array, and then the new value is added. Each time after that, simply push the new value to the already existing array:
I learned this trick from maverick many months ago (we both use CGI.pm now, for the record). I only show this in hopes to promote the learning of anonymous data structures and advocate using CGI.pm instead. :)if (defined $params{$key} && ref($params{$key}) eq "ARRAY") { # key has been seen at least twice now # the value is a an anonymous array, push the new value push (@{$params{$key}}, $val); } elsif (defined($params{$key})) { # second time key has been seen # the value is a scalar, 'turn' it into an anonymous array $params{$key} = [$params{$key}, $val]; } else { # first time this key has been seen # assign the value as a scalar $params{$key} = $val; }
Jeff
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Multiple Data Selections per Field in CGI
by salvadors (Pilgrim) on Jan 29, 2001 at 02:27 UTC |