in reply to Re: Handling Dynamic URL Parameters
in thread Handling Dynamic URL Parameters

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.

Replies are listed 'Best First'.
Re3: Handling Dynamic URL Parameters
by Solo (Deacon) on Sep 13, 2002 at 18:06 UTC
    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.