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

my @gene1 = $query->param('GS1');

This took parameter GS1 from the form and put it into the first element of array @gene1.

@gene1 = split ('', $gene1);

This takes scalar $gene1 and attempts to split it into the characters it contains. Since no assigment has been made to $gene1, you get an empty result back. This clears the array @gene1. Also, I doubt this code compiles since you never declared $gene1 and your code include use strict;. Are you getting http 500 errors?

print STDOUT $gene[0];

This attempts to print an element from array @gene which has never been declared nor assigned to. Of course, nothing gets printed and this should also cause a compilation error due to not being declared.

Maybe what you wanted was this.

my $gene1 = $query->param('GS1'); my @gene1 = split('',$gene1); print STDOUT $gene1[0];

This is still somewhat broken since you output a header declaring your document to be text/html but you don't have any html tags in the output.

--- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

Replies are listed 'Best First'.
Re: Re: Reading multiple values from a selection with CGI.pm
by roundboy (Sexton) on Jan 22, 2003 at 18:17 UTC

    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