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

Monks of Perl,
I am attempting to parse multiple values from one form to the next. I have placed hyphens (-) between the values so i can split them and put them into an array. Next i want to compare each element of the array to a value bound from a statement handler. However when performing this it will only match the first element. This is frustrating as when i print the other elements they appear to be the same.

In order to test the loop the values were entered manually, and produced the desired results. This leads me to believe the that the problem is related to the parsing of the values.

any help would be greatly appreciated.

below is some code to demonstrate the problem

# $sites - this variable contains values parsed from the previous form
@array=split( " ", $sites);
if ($array[0] eq "three")
{
print "three ";
}
if ($array1 eq "seven")
{
print "seven ";
}

Replies are listed 'Best First'.
Re: Parsing Arrays
by merlyn (Sage) on Dec 20, 2000 at 08:10 UTC
    If you use the CGI.pm shortcuts, you can just dump a hidden field with an arrayref for a value, and it'll show up as a multivalue field on reply:
    param("basket", 1..10); ... print hidden("basket");
    and in response you'll get:
    my @list = param("basket"); # @list = (1..10);

    -- Randal L. Schwartz, Perl hacker

Re: Parsing Arrays
by a (Friar) on Dec 20, 2000 at 09:23 UTC
    Not sure I'm w/ you: you say you used '-' to separate, but you split on a space (split(" ",$sites), but: this should work (you'll want to use code and /code tags to make your code readable: see writeup formatting tips):
    $sites = "one-seven-three-four-nine"; @site_list = split(/-/,$sites # array is a bad array name foreach $site ( @site_list ) { # assuming you've got something more in mind # than print "$site\n"; if ( $site eq "three" ) { print "we got a three\n"; } if ( $site eq "seven" ) { .... } # foreach $site

    a

Re: Parsing Arrays
by OeufMayo (Curate) on Dec 20, 2000 at 15:03 UTC
    I guess that with the -w switch and use strict, you could have save yourself some trouble!
    @array=split( /-/, $sites); # from perldoc, it seems better to use a p +attern rather than a string if ($array[0] eq "three") # $array0 != $array[0] { print "three "; } if ($array[1] eq "seven") { print "seven "; }
    -- PerlMonger::Paris(http => 'paris.pm.org');
Re: Parsing Arrays
by onion2k (Novice) on Dec 20, 2000 at 16:22 UTC
    Morning,

    A couple of minor points. Firstly, using 'split(" ", $sites);' is going to split the variable up by spaces rather than '-'s. Simple fix though. (I guess its a typo?).

    Secondly, grab yourself an HTML reference and read up on hidden form fields. The syntax is simply '<input type="hidden" name="varname" value="varvalue">' By replacing varvalue with an actual variable, and then using the form reply the information persists across the web pages. This can also be done using the CGI.pm modules 'print hidden' thingie.

    Chris

    $live{$free}||die