"N" number of parameters using cgi
How do I get the number "N" using cgi?
Current I am using the code below. But I don't know the name of the parameter. In this case, how to do it?
Code: script.cgi
use strict;
use CGI;
my $query = new CGI;
my $paramValue1 = $query->param('name1');
my $paramValue2 = $query->param('name2');
.....
Enter the URL:
http://host/cgi-bin/script.cgi?name1=value1&name2=value2&.........
Output value:
value1,value2,.....
Please help me with this. Thank.
+3
source to share
3 answers
Below is bad practice:
my $name1 = $query->param('name1');
my $name2 = $query->param('name2');
Use an array!
my @names;
push @names, $query->param('name1');
push @names, $query->param('name2');
To your question: how to avoid hardcoding all indexes, which is especially important since the number of names is variable.
$query->param
without arguments returns a list of supplied parameters, so it becomes a matter of filtering and sorting. You want the following:
my @names =
map { $query->param('name'.$_) }
sort { $a <=> $b }
map { /^name(\d+)\z/ ? $1 : () }
$query->param;
It would make more sense if the URL was
http://host/cgi-bin/script.cgi?name=value1&name=value2&...
Because if you have this url you could just use
my @names = $query->param('name');
+3
source to share