Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Parsing cgi variable lists

by inblosam (Monk)
on Jun 29, 2005 at 13:10 UTC ( [id://471007]=perlquestion: print w/replies, xml ) Need Help??

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

I use Mason for my websites, so I don't usually run into the issue of getting variables. However, I need to dynamically get my variables (so I don't have to initialize each one).
<form method="get" action="test_list.html"> <input type="hidden" name="LineItem" value="1" /> First Number: <input type="text" name="first-1" /><br /> Second: <input type="text" name="second-1" /><br /> Third: <input type="text" name="third-1" /> </p> <p> <input type="hidden" name="LineItem" value="2" /> First Number: <input type="text" name="first-2" /><br /> Second: <input type="text" name="second-2" /><br /> Third: <input type="text" name="third-2" /> </p> <input type="submit" name="a" value="go"> </form>
From a form like that I need to keep the components together that end in the same number so I can do something with it from the database. I found a mailing list post that suggested the following, but I kept getting an error from it:
foreach (values $ARGS{LineItem}) { $SQL->execute( $PONum, $ARGS{"first-${i}"}, $ARGS{"second-${i}"}, $ARGS{"third-${i}"} ); }
I tried a few things but my main problem was getting the LineItem variable into an array (not an array of arrays). So it only gets one of the sets of variables.
my @lineitems = $ARGS{"LineItem"}; foreach my $line (@lineitems) { print "line is @$line[0]"; print $ARGS{"first-@$line[0]"}; print $ARGS{"second-@$line[0]"}; print $ARGS{"third-@$line[0]"}; }
I am guessing I am getting CLOSE, but I can't quite complete it without some help. Thanks!


Michael Jensen
inblosam.com

Replies are listed 'Best First'.
Re: Parsing cgi variable lists
by tlm (Prior) on Jun 29, 2005 at 13:19 UTC

    What errors are you getting? I don't know the first thing about Mason, but this bit

    values $ARGS{LineItem}
    looks wrong, because the argument to the values builtin should be a hash, and $ARGS{ LineItem } is a scalar. Maybe you want something like this?
    values %{ $ARGS{ LineItem } }
    Likewise, I suspect this
    my @lineitems = $ARGS{"LineItem"};
    is not doing what you want (it creates an array of one item, which, in this case at least, would not warrant iterating over with a foreach loop). Maybe you want
    my @lineitems = @{ $ARGS{ LineItem } }
    but both this and my earlier suggestion can't both be right (Perl doesn't permit something that is a reference to both an array and a hash).

    There are other basic syntax errors in your code, which suggests to me that reading perldata, perlreftut, and perlref would be useful to you. Otherwise I think you will be "programming by trial and error", which is not only painful but dangerous, because your only indication of success is when the thing "runs" and terminates normally. A program can easily pass this test and still be completely wrong.

    Also, from your code it is not clear to me what $i (i.e. ${i}) is.

    the lowliest monk

Re: Parsing cgi variable lists
by fmerges (Chaplain) on Jun 29, 2005 at 13:50 UTC
Re: Parsing cgi variable lists
by tcf03 (Deacon) on Jun 29, 2005 at 13:21 UTC
    foreach (values $ARGS{LineItem}) { $SQL->execute( $PONum, $ARGS{"first-${i}"}, $ARGS{"second-${i}"}, $ARGS{"third-${i}"} ); }
    Where is $i coming from shouldn't you be using foreach my $i (values $ARGS{LineItem}) I may be missing something though.

    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
Re: Parsing cgi variable lists
by inblosam (Monk) on Jun 29, 2005 at 14:51 UTC
    Okay, I tried an above suggestion and this worked out:
    my @lineitems = @{ $ARGS{ LineItem } }; foreach my $line (@lineitems) { print "line is $line"; my $first = $ARGS{"first-$line"}; my $second = $ARGS{"second-$line"}; my $third = $ARGS{"third-$line"}; print "first is $first"; print "second is $second"; print "third is $third"; }

    This appears to be working now. Thanks for the help!


    Michael Jensen
      In the future (and in general cases where data structure isn't known), a simple
      use Data::Dumper; warn Dumper \%ARGS; warn Dumper $ARGS{LineItem};
      would have show that it was an array ref, leading immediately to the @{$ARGS{LineItem}} solution.

      Also, in mason you can do this:
      <%args> $LineItem=>[] </%args> <%init> warn join ":", @$LineItem; ... </%init>
      That takes care of the case where just one LineItem form element exists. Using that syntax, you'll still get an array ref. Using the above $ARGS{LineItem} syntax you'll get a scalar, which will make the @{$ARGS{LineItem}} solution error out.
Re: Parsing cgi variable lists
by inblosam (Monk) on Jun 29, 2005 at 13:32 UTC
    I wasn't sure about the code from the mailing list either (like where did the $i come from etc) but I thought I would post it because it seems to be a decent attempt.

    The error I was getting with the mailing list code (beside not initializing the $i) was "Type of arg 1 to values must be hash (not hash element)".

    I will give it a try with your suggestions, but any more are welcome for the optimum way to do this. Thanks!


    Michael Jensen

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://471007]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-28 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found