如何在Perl CGI HTML模板的下拉列表中获取数组值?

时间:2016-01-11 16:09:55

标签: perl

请使用HTML模板建议如何在下拉列表中获取数组值。

use HTML::Template;
open (FL, "<file.txt"); 

#(file.txt values are below)
#count1
#count2
#count3
#count4
#count5 ## samples

my @TOTAL = <FL>; 

foreach $count(@TOTAL) {
    chomp $count;
    $template->param( COUNT => [{name => $count}]); # here I am getting only one value in  the drop down menu (count1 value only)
}

我期待下面的值,因此下拉列表会列出所有值。

$template->param(COUNT => [{name => $count1}, {name => $count2}, {name => $count3}, {name => $count4}]);

print $template->output, "\n";

1 个答案:

答案 0 :(得分:3)

你可以map it,然后就不需要循环了。

$template->param( 
  COUNT => [ 
    map { 
      { name => $_ } 
    } @TOTAL 
  ]
);

外部{}用于map需要的,内部{}是哈希引用。

但请不要忘记chomp your input