1

我正在使用一个新的支付处理器,他们的数据表只是说他们的信息是作为多维数组发布的。仅引用几个变量:

products[x][prod_number]
products[x][prod_name]
products[x][prod_type]

** 有十个这样的数组

我发现如果该人订购 3 件商品,则有一个名为“item_count”的变量,这意味着 X 变为 [0]、[1] 和 [2]

但是在这个 POSTed 数据中读取并分离它的方法是什么。当然这将是某种“foreach”循环,但我需要什么变量名是个谜

对于普通变量,即获取“名称/值”对,我使用这个:

use CGI qw/:standard/;

@names=param;
foreach $name(@names){
$value=param($name);
$$name=$value;
}

任何指针?

+++++

不确定这是否是添加到此帖子的正确方法;我还在学习系统

我现在的问题是这些数据以什么格式发布到标准输入等。或者更重要的是,它将被读入什么。由于“products”是一个单一的变量名,所有数据是在一个单一的“$products”变量中,还是所有数据都包含在@products 中?

4

3 回答 3

2
my $num_items = $cgi->param('item_count');

my @ordered_prods;
for my $ordered_prod_num (1..$num_items) {
   my %ordered_prod;
   for my $field_name (qw( prod_number prod_name prod_type )) {
      $ordered_prod{$field_name} =
         $cgi->param("products[$ordered_prod_num][$field_name]");
   }

   push @ordered_prods, \%ordered_prod;
}

或者转念一想,

my @ordered_prods;
for my $param_name ($cgi->param()) {
   if (
      my ($ordered_prod_num, $field_name) =
         $param_name =~ /^products\[([0-9]+)\]\[(\w+)\]\z/
   ) {
      $ordered_prods[$ordered_prod_num]{$field_name} =
         $cgi->param($param_name);
   }
}
于 2013-10-22T15:29:06.620 回答
0

通过开发其他人可能想尝试的小脚本,我设法解决了多维数组的工作原理。(我可以离线测试这个,因为我在我的 Window 的 PC 上运行了一个 PERL。毫无疑问,其他人可以通过类似的环境进行测试)

基本上,我从另一张海报中复制了一个关于如何发布数组的“问题”,并构建了一个脚本来分析从 HTML 表单发送到服务器脚本的内容。然后,我在上面发布的答案的帮助下添加了自己的解码程序。就“纯”编程而言,它可能很粗糙,但可以逐步研究它,看看发生了什么:(我更喜欢的工作方式!)

 #!/usr/bin/perl
use CGI qw/:standard/;
print "content-type: text/html\n\n";
use CGI::Carp qw( fatalsToBrowser );

if (param('action') eq '1'){    # Ignore this: Simply to help demo work
&output;
}



## The form window
## Allows two people to input required "diameters"

print <<EOF;
<form method="post" action="http://www.MY-DOMAIN.com/cgi-bin/test_multi.pl">    <!--Post to your own domain-->
<input type="hidden" name="action" value="1">                   <!-- Ignore: Simply helps demo script along-->
<table>
<tr>
<td>Customer 0: Top diameter<input name="diameter[0][top]" type="text" size="5"></td>
  <td>Customer 0:Bottom diameter<input name="diameter[0][bottom]" type="text" size="5"></td>
</tr>
<tr>
  <td>Customer 1: Top diameter<input name="diameter[1][top]" type="text" size="5"></td>
  <td>Customer 1: Bottom diameter<input name="diameter[1][bottom]" type="text" size="5"></td>
</tr>
</table>
<input type="submit" value="send form">
</form>
EOF



sub output{         # Ignore fact that it's in sub-routine: Just makes demo easier
print "These are the \"name/value\" pairs as seen by the input to script,<br>ie what form sends to &lt;stdin&gt;<br><b>Note: The \"|\" symbol is added by script for clarity</b><br><br>";
@names=param;
foreach $name(@names){
$value=param($name);
print "$name=$value|";      #Visual reminder of what's happening
}

print "<hr>";

$item_count=1;                  # ONE LESS than less than total number of "levels" in HTML form, ie Customer[0], Customer[1] = 1
@field_name=('top','bottom');           # Fields as defined in form

foreach  $i($item_count){           # Loop through each of the levels, (Customer[0] and Customer[1] in this example)
    foreach $x(@field_name){        # Loop through the fields within each level ([top] and [bottom] in example)
$name=$field_name[$x];              # Places the field name[$x] in the variable "name"
$value=param("diameter[$i][$field_name[$x]]");  # The value of the array is assigned to a variable "value" 
$$name=$value;                  # The name is assigned to a variable, and given the value (ie "$top=xx" "$bottom=zz")
print "Customer $i \$$name=".$value."<br>"; # Visual reminder of what's happening
    }
# Process this loop, and do something with data before moving to next loop
# Values on first loop:  "$i = 0", "$top=xx" and "$bottom=zz" with values of the first array
# Value on second loop: "$i=1", "$top=xx" and "$bottom=zz" with values of the second array
}
print "<hr>";

# The names of "diameter", "top" and "bottom" reflect the names used in the form and would be changed to suit requirements.
# The advantage of this script is it assigns values back to single variables, 
# and avoiding getting in a muddle trying to keep track of which element of [A][B] array one is working on at any time 
}

希望这可以帮助其他人了解阵列的工作原理。

注意:似乎多维数组是完整发送的,即:diameter[0][top]=x&diameter[0][bottom]=z&diameter[1][top]=x&diameter[1][bottom]=z,带有正方形成对的括号

于 2013-10-26T14:51:40.800 回答
0

接收 CGI 参数的更好方法:

哈希参考:

my $params = { map { $_ => ($cgi->param($_))[0] } $cgi->param };

直哈希:

my %params = map { $_ => ($cgi->param($_))[0] } $cgi->param; 

或僧侣模式;)

sub Vars { 
    my $q = shift; 
    my %in; 
    tie(%in,CGI,$q); 
    return %in if wantarray; 
    return \%in; 
} 
于 2013-10-22T15:32:38.970 回答