in reply to Multiple Data Selections per Field in CGI

I would like to say just use CGI and be done with it. But that's too easy. I'd rather talk about the problem at hand.

So you are only getting the last element, eh? This leads me to believe that you are using something like this:

split up query string and decode pairs into $val and $key insert $key into params hash with value $val
That doesn't work when you have a query string like
foo.cgi?foo=mashed&foo=fried&foo=boiled&foo=tofu
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.

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:

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; }
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. :)

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

    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:

    You could also do this quite nicely with a tie:

    package Quantum::Heisenberg; sub TIESCALAR { bless [], $_[0] } sub STORE { push @{$_[0]}, $_[1] } sub FETCH { my $self = shift; $self->[int rand @$self]; } 1;

    Changing the FETCH method to do what you'd expect is left as an exercise for the reader ;)

    Tony