提问人:Alexx 提问时间:10/29/2020 最后编辑:brian d foyAlexx 更新时间:12/27/2020 访问量:311
如何在Perl中删除这些警告?[关闭]
How to remove these warnings in Perl? [closed]
问:
在这里,我正在检查每列中存在的最大字宽
my @col_lns;
while (<file>) {
my @row = split " ",$_;
@col_lns = map ((length) @rows) if $. ==1;
for ( my $col_l =0; $col_l <$#row; $col_l+=1) {
my $col_ln = length $row[$col_l];
if ($col_lns[$col_l] < $col_ln) ###Here I am getting warning
{
$col_lns[$col_l] = $col_ln;
}
}
警告 1:
Use of uninitialized value in numeric lt (<)
代码第 2 部分;
my $pack1 = substr($add,4,4);
my $pack2 = substr($add,0,4);
警告 2
Use of $add in substr
substr outside of string
答:
4赞
Dave Cross
10/29/2020
#1
自己诊断其中任何一个真的不难。
在数字 lt 中使用未初始化的值 (<)
您知道发出此警告的代码段:
if ($col_lns[$col_l] < $coln)
因此,在执行此行时,要么包含。如果我无法找出问题所在,我会在执行该行代码之前添加一个显示这两个值的 print 语句。我可能还会加入以显示我们正在查看的数组的哪个元素。$col_lns[$col_l]
$coln
undef
$col_l
鉴于您上一个问题中的混淆,以及您仍在使用建议您不要使用的 C 样式循环这一事实,我很确定这是由于您偏离了数组的末尾造成的。for
在substr中使用$add
字符串外部的 substr
那里的第一个警告很奇怪。对我来说,这看起来不像是Perl的警告。你编辑得太多了吗?但第二个很清楚。您正在尝试获取子字符串,但使用的值超出了源字符串的长度。同样,可以通过打印代码中使用的值来调查此问题。
值得一提的是,有一个 perldiag 手册页,其中包含了 Perl 的所有错误和警告,以及对它们含义的更详细的解释。您可以通过向代码添加使用诊断
来在运行时显示这些说明 - 但请在问题修复后将其删除。
评论
1赞
ikegami
10/30/2020
回复 “那里的第一个警告很奇怪。[...]你是不是编辑得太多了?“,肯定应该是。这也解释了随后的警告。的字符串化是空字符串,因此等价于(除了警告),导致第二个警告。关键是,修复第一个可能会修复第二个。Use of uninitialized value $add in substr
undef
substr(undef,4,4)
substr("",4,4)
评论
my $col_ln
$coln
use strict
Use of $add in substr
看起来不像是一个真正的警告。看起来您剪掉了警告的一部分,例如 ,这将解释另一个错误(未定义的字符串长度为 0)。Use of uninitialized value $add in substr
use strict
{ length }
@rows 或map(length, @rows)”,
您仍然可以使用第一种语法的 parens。所以也许他们想要map( { length } @rows )