in reply to Handling Dynamic URL Parameters

Append line numbers to the fields so I can bypass hash and array manipulation entirely. i.e item_no_1=1&item_no_2=2&item_no_3=3&qty_1=11&qty_2=12&qty_3=13)
I make heavy use of this approach with sometimes more complex 'keys'. For instance:
use strict; use warnings; use CGI; my $q = CGI::new; # or however you get your $q ... # input names look like "id1~id2~id3" # or pick a delimiter you like instead of ~ for my $param ($q->param) { # print "$param<br/>\n"; next unless $param =~ /.*(?:~.+){2}/; my ($id1,$id2,$id3) = split(/~/,$param); # print "$id1 - $id2 - $id3<br/>\n"; # ... }
(Edit: minor code addition, use strict, etc. :)
--
May the Source be with you.

You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.

Replies are listed 'Best First'.
Re: Re: Handling Dynamic URL Parameters
by the_Don (Scribe) on Sep 13, 2002 at 17:28 UTC

    I'm not familiar with how CGI returns values, and I dont understand how this script works exactly.

    for my $param ($q->param)
    This gets an array of all the parameter names

    next unless $param =~ /.*(?:~.+){2}/;
    This checks for zero or more of any character followed by 2 occurances of either ?:~.+

    my ($id1,$id2,$id3) = split/~/,$param);
    $param is a string of values and you spilt on the ~

    I guess my confusion is on the distinction between parameter name and values here. Could you give an example URL that you would pass to have this script work?

    the_Don
    ...making offers others can't rufuse.

      This checks for zero or more of any character followed by 2 occurances of either ?:~.+
      The (?: ... ) construction is 'pure grouping.' So (?:~.+){2} matches, but does not assign to a variable, a tilde ~ followed by one or more chars twice.... basically checking for 2 tildes. Somebody else could write this more elegantly, I'm sure.

      Here's an example querystring. I'll use the names that shotgunefx suggested.

      ?item~1~name=boobtube&item~1~qty=1&item~1~code=mysqlpk&item~2~name=loo +sifer&item~2~qty=1&item~2~code=666&ship~firstname=Han ...
      Since now we're dealing with fields with 2 and 3 ids, we need to adjust the code some.
      # Always use warnings; use strict; use CGI; my $q = CGI::new; my %lineitem; my %ship; # process params # for my $param ( $q->param ) { my @id = split(/~/,$param); # a SWITCH would work nicely, but I'll try # not to confuse the example if ( $id[0] eq 'item' ) { $lineitem{$id[1]}{$id[2]} = $q->param($param); } if ( $id[0] eq 'ship' ) { $ship{$id[1]} = $q->param($param); } } # process lineitems # print "Please confirm order of<br/> "; for my $item ( keys %lineitem ) { print $lineitem{$item}{qty}, $lineitem{$item}{name}; print "<br/>"; } print "<br/>being shipped to $ship{firstname}<br/>";
      Is this a better example?

      (Edit: minor spelling and grammer fixes)
      (Edit: replaced my ($id1,$id2,$id3) = with my @id =)

        Yes. Much better. Worth some random ++ as far I am concerned.

        the_Don
        ...making offers others can't rufuse.