【发布时间】:2012-08-19 00:22:15
【问题描述】:
我正在编写一个 Perl 脚本,但我遇到了一个我无法真正克服的问题。这是我的代码:
my @rowses = ();
while ( @list = $sth->fetchrow_array())
{
%row = ();
if($list[30] == 1)
%row = (
cod_cliente => $list[1],
rag_soc => $list[2],
p_iva => $list[11],
IDanagrafica => $list[0],
tabella => $tab,
IDanagraficaE => $list[0],
tabellaE => $tab,
checkbox => "checked",
);
$LOL = \%row;
print $cgi->p($LOL);
}
else
{
%row = (
cod_cliente => $list[1],
rag_soc => $list[2],
p_iva => $list[11],
IDanagrafica => $list[0],
tabella => $tab,
IDanagraficaE => $list[0],
tabellaE => $tab,
checkbox => "",
);
$LOL = \%row;
print $cgi->p($LOL);
}
push (@rowses, \%row);
}
$template->param(table => \@rowses);
$template->param(tab => $tab);
当我为了调试而尝试打印对一行的引用 ($LOL) 时,它什么也不打印,当我在 @rowses 打印引用时,它是一个充满所有相同哈希的数组,最后一个从语句中获取的。
奇怪的是,如果我每次打印一个哈希行,而不引用它,它会很好地打印它们,并且全部打印出来。
我这样做是为了将包含所有哈希的数组引用传递给 TMPL_LOOP,并打印它们;但它会打印一个仅包含最后一行的长列表。
提前感谢所有帮助我的人。
【问题讨论】:
-
if($list[30] == 1) 之后是否缺少花括号?
-
您可以通过以下方式简化该条件:
%row = (cod_cliente => ..., tabellaE => $tab); $row{checkbox} = ($list[30] == 1 ? "checked" : "");
标签: arrays perl templates hash dbi