Perl:谁能告诉我如何解决这个警告 [英] Perl : Can anyone tell me how to resolve this warnings

查看:57
本文介绍了Perl:谁能告诉我如何解决这个警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码第 1 部分:

      my $length = @array;

269   for (my $j=1; $j <= $length; $j+=1) {
270
271     if ( $fields[$j] =~ /dat/) {
    
         }}

警告 1:

Use of uninitialized value within @array in pattern match (m//) at wrk.pl line 270

代码第 2 部分:这里我尝试将十进制转换为十六进制

Code part 2: Here I am trying to convert the decimal to hexadecimal

70 while (my $line = <DATA>) {
71     $line =~ s/ '([0-9]*)' / sprintf '0x%x', $1/eg;
72     print OUT $line;
       }

警告 2:

Argument "" isn't numeric in sprint at wrk.pl line 71

更新

在我把 for (my $j=1; $j <= $#array; $j+=1) 和第二个警告我改变 后,上面的两个警告现在已经解决了$line =~ s/'([0-9]+)'/sprintf '0x%x', $1/eg; .

Above both warnings has now resolve after I put for (my $j=1; $j <= $#array; $j+=1) and for 2nd warning I change $line =~ s/ '([0-9]+)' / sprintf '0x%x', $1/eg; .

我又收到两个警告

代码部分 3:我在这里检查每列中存在的最大字宽

Code part 3: Here I am checking the max word width present in each columns

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] < $coln)    ###Here I am getting warning
{
  $col_lns[$col_l] = $coln;
}
}

警告 3:

Use of uninitialized value in numeric lt (<) 

代码第 4 部分;

my $pack1 = substr($add,4,4);
my $pack2 = substr($add,0,4);

警告 4

Use of $add in substr
substr outside of string 

推荐答案

写下代码的分步演练.(感谢池上在评论中提到了其中的一些内容)

Writing down step by step walkthrough of the code. (Credit to ikegami for mentioning some of this in comments)

代码第 1 部分:

这部分有问题:

  • 您检查数组 @array 的长度,然后使用该长度循环遍历 @fields 数组.
  • 您从数组索引 1 开始,然后转到等于数组长度的数组索引(假设使用了正确的数组长度).这是一次性错误.数组从 0 开始.您应该从 0 循环到数组 $#fields 的最大 index(比长度少一个).
  • 警告 1 与代码中所写的不符.错误应该是关于数组@fields,这是你在代码中使用的.
  • 警告 1 给出了错误的行号 270.(最后两点说明你在复制错误信息后修改了代码,这是不好的做法)
  • 警告 1 表示 @array 数组中的一个值未定义.由于循环条件中存在一次性错误,因此可以很好地猜测未定义值位于数组的末尾,一旦修复循环条件,此警告就会消失.
  • You check the length of array @array, then use that length to loop over the @fields array.
  • You start at array index 1 and go to array index equal to array length (assuming the correct array length was used). This is a one-off error. Arrays start at 0. You should loop from 0 to max index of array $#fields (one less than length).
  • Warning 1 does not match what is written in the code. The error should be about array @fields, which is what you use in the code.
  • Warning 1 gives the wrong line number 270. (These last two points indicate that you have altered the code after copying the error message, which is a bad thing to do)
  • Warning 1 means that one value in the @array array is undefined. Since you have a one-off error in the loop conditions, a good guess is that the undefined value is at the end of the array and that this warning disappears once you fix the loop condition.

代码第 2 部分:

  • 您正在将空字符串 "" 提供给 sprintf,它会就此向您发出警告.由于您的匹配项被空格和单引号包围,因此输入中可能是一个空字符串(即 '').为了避免匹配,您可以使用 + 而不是 *,即 [0-9]+.量词 + 表示匹配 1 次或多次",而 * 表示匹配 0 次或多次".请注意,这样做也会留下引号和空格,它们在其他时候会被删除.
  • DATA 是文件内数据的保留文件句柄,使用文件底部的 __DATA__ 标记.除非这是您正在执行的操作,否则您应该选择另一个文件句柄.最好是一个词汇,例如我的 $fh.
  • You are feeding the empty string "" to sprintf, and it warns you about that. Since your match is surrounded by space and single quotes, it is likely an empty string in the input (i.e. ''). To avoid matching that, you can use + instead of *, i.e. [0-9]+. The quantifier + means "match 1 or more times", whereas * means "match 0 or more times". Be aware that doing this will also leave the quotes and spaces, which are removed at other times.
  • DATA is a reserved file handle for in-file data, using the __DATA__ marker at the bottom of the file. Unless this is what you are doing, you should choose another file handle. Preferably a lexical one, e.g. my $fh.

您进一步评论:

@ikegami,更改后仍然收到警告.

@ikegami, still getting warnings after changes.

由于您没有提及这些警告是什么,我们无法为您提供帮助.

Since you do not mention what those warnings are, we cannot help you.

这篇关于Perl:谁能告诉我如何解决这个警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆