Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

accessing random count CGI parameters

by anothersmurf (Novice)
on Jun 25, 2007 at 02:20 UTC ( #623095=perlquestion: print w/replies, xml ) Need Help??
anothersmurf has asked for the wisdom of the Perl Monks concerning the following question:

My CGI app is called like this: "x.cgi?xxxc=2xxx1=val1&xxx2=val2"

xxxc would be the count, which would be followed by the two values.

The CGI app parses the input as such:
$my_input = $ENV{QUERY_STRING}; @fv_pairs = split /\&/ , $my_input; foreach $pair (@fv_pairs) { if($pair=~m/([^=]+)=(.*)/) { $field = $1; $value = $2; $value =~ s/\+/ /g; $value =~ s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg; $INPUT{$field}=$value; } }
How can I take $INPUT{xxxc}, and use the count to sequentially access $INPUT{xxx1} and $INPUT{xxx2} as it is up to hundreds of entries. The real app recieves data via POST not GET, so don't worry about the max URL length. =)
Thanks!
-JNY

Replies are listed 'Best First'.
Re: accessing random count CGI parameters
by naikonta (Curate) on Jun 25, 2007 at 03:21 UTC
    I'd just probably use the "normal" procedur:
    #!/usr/bin/perl use strict; use warnings; use CGI qw(:standard *ul); print header; my $count; unless ($count = param('xxxc')) { print b("There's no count for you"); exit; } print b("We have $count input(s) as the following:"), start_ul; my %inputs; for my $i (1 .. $count) { my $field = "xxx$i"; my $value = param($field); $value = 'yes!' unless defined $value; $inputs{$field} = $value; # stored for later use print li("$i = $value"); } print end_ul; # use %inputs for whatever you like
    Accessed as http://localhost/cgi-bin/input.cgi?xxxc=4&xxx1=Perl&xxx2=for&xxx3=all, output on the browser is:
    We have 4 input(s) as the following: * 1 = Perl * 2 = for * 3 = all * 4 = yes!
    And yes, it works for both GET and POST request methods.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: accessing random count CGI parameters
by CountZero (Bishop) on Jun 25, 2007 at 05:50 UTC
    You don't need the "xxxc=2" part of the query string as the CGI-module will know all by itself the number of parameters input, unless you use that value as some sort of (weak) checksum.

    Anyhow it is far better to rely on CGI.pm (or its brethren such as CGI:Simple) to do the parsing.

    And in order to sequentially access the parameters just sort the key-values of the parameter-hash and use that sorted list to go through the values of the parameters.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      And in order to sequentially access the parameters just sort the key-values of the parameter-hash and use that sorted list to go through the values of the parameters.

      ... just to extend that - "sort numerically", as in

      perl -le 'print for sort {$a <=> $b} qw(bbb1 aaa2 ccc3 xxx15 def159)' bbb1 aaa2 ccc3 xxx15 def159

      So the snippet would be

      foreach my $key ( sort {$a <=> $b} $q->param() ) { my $value = $q->param($key); # now do whatever with $value ... }

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: I think I've stepped in it now
by GrandFather (Sage) on Jun 25, 2007 at 02:34 UTC

    Do you mean something like:

    use strict; use warnings; my $my_input = 'xxxc=2&xxx1=val1&xxx2=val2'; my @fv_pairs = split /\&/ , $my_input; my %INPUT; foreach (@fv_pairs) { my ($field, $value) = split /=/, $_, 2; next unless defined $value; $value =~ s/\+/ /g; $value =~ s/%(\dA-Fa-f{2})/pack("C", hex($1))/eg; $INPUT{$field}=$value; } print join "\n", map {"$_ => $INPUT{$_}"} sort keys %INPUT;

    Prints:

    xxx1 => val1 xxx2 => val2 xxxc => 2

    DWIM is Perl's answer to Gödel
Re: accessing random count CGI parameters
by shmem (Chancellor) on Jun 25, 2007 at 11:05 UTC

    First, don't process the QUERY_STRING by hand! Let CGI do that. For instance, parameters might get passed using ';' as a separator rather than '&'. See Ovid's CGI Course for more on that.

    That said, the following would do

    use CGI; my $q = CGI->new; my @list = map { $q->param('xxx'.$_) } 1 .. $q->param('xxxc'); # values now sequential in @list.

    For not duplicating the values into @list, use

    foreach my $index (1 .. $q->param('xxxc') ) { my $value = $q->param('xxx' . $index); # now do whatever with the value ... }

    If you absolutely want your %INPUT hash, you get it populated with

    %INPUT = $q->Vars;

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://623095]
Approved by ww
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2018-07-23 03:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (459 votes). Check out past polls.

    Notices?