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)

In reply to (jeffa) Re: Multiple Data Selections per Field in CGI by jeffa
in thread Multiple Data Selections per Field in CGI by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.