Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have written many a CGI in my time but have never incorporated 'multiple selects' in them. This is causing me grief. I can read the multiple selected values in fine into an array but can't split them! I have tried to 'split' the values on many characters but nothing is doing the job. Can anyone help? Also, how could I restict the user to selecting only 2 values from the list?? Heres a snippet of the code.
#! /usr/bin/perl -w use strict; use CGI; my $query = new CGI; print STDOUT $query->header(); print STDOUT $query->start_html; my @gene1 = $query->param('GS1'); @gene1 = split ('', $gene1); print STDOUT $gene[0];

edited: Wed Jan 22 23:37:47 2003 by jeffa - title change (was: CGI fun!)

Replies are listed 'Best First'.
Re: Reading multiple values from a selection with CGI.pm
by pfaut (Priest) on Jan 22, 2003 at 15:04 UTC
    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';

      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

Re: Reading multiple values from a selection with CGI.pm
by cfreak (Chaplain) on Jan 22, 2003 at 15:02 UTC

    The CGI module automagically puts them into an array for you. You don't have to split it.

    Just take out the split line

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

    Also just out of curiosity why are you using STDOUT in your print statement (STDOUT is default)

    Hope that helps
    Chris
    Lobster Aliens Are attacking the world!
Re: Reading multiple values from a selection with CGI.pm
by OM_Zen (Scribe) on Jan 22, 2003 at 18:54 UTC
    Hi ,

    The $gene1 , did you think of doing this

    my $gene1 = $query->param('GS1'); @gene1 = split('',$gene1);
    is the STDOUT an explicit file handler, if so the name should be different , else if you have to print it out the default is STDOUT ,the use is redundant .to note, the split is actually on a string and not an array