in reply to Re: Reading multiple values from a selection with CGI.pm
in thread Reading multiple values from a selection with CGI.pm

If you are only dealing with a single CGI parameter at a time, (i.e., are calling CGI::param with an argument), then the following snippet from 'perldoc CGI' is relevant:


FETCHING THE VALUE OR VALUES OF A SINGLE NAMED PARAMETER:

@values = $query->param('foo'); -or- $value = $query->param('foo');
Pass the param() method a single argument to fetch the value of the named parameter. If the parameter is multi- valued (e.g. from multiple selections in a scrolling list), you can ask to receive an array. Otherwise the method will return a single value.

Note that this means that if you call $query->param('GS1') in a scalar context, it will return a single value, regardless of how many values were set in the form.

On the other hand, if you obtain all your parameters and their values at once, using, e.g., my $form = $query->Vars() (which makes $form a reference to an anonymous hash, of which the keys are the form parameter names), then the values will all come out as scalars. In this case, multi-valued parameters have their values represented as a packed string, with the individual values separated by "\0" (the ascii NUL character). So in that case:

my $form = $cgi->Vars(); foreach my $param (keys %$form) { my @values = split /\0/, $form->{$param}; print "$param values are: ", join(', ', @values), "\n"; }

The CGI perldocs are long, comprehensive, and useful. I highly recommend giving them a thorough read.

HTH,
--roundboy