如何在 Perl 中删除这些警告? [英] How to remove these warnings in Perl?

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

问题描述

这里我正在检查每列中存在的最大字宽

我的@col_lns;而(<文件>){我的@row = split "",$_;@col_lns = map ((length) @rows) 如果 $.==1;for ( 我的 $col_l =0; $col_l <$#row; $col_l+=1) {我的 $col_ln = 长度 $row[$col_l];if ($col_lns[$col_l] < $col_ln) ###Here 我收到警告{$col_lns[$col_l] = $col_ln;}}

警告 1:

在数字 lt (<) 中使用未初始化的值

代码部分 2;

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

警告 2

在substr中使用$add字符串外的 substr

解决方案

自己诊断其中任何一个都不难.

<块引用>

在数字 lt (<) 中使用未初始化的值

您知道发出此警告的代码段:

if ($col_lns[$col_l] < $coln)

因此,当您执行此行时,$col_lns[$col_l]$coln 包含 undef.如果我无法弄清楚问题是什么,我会添加一个打印语句,在执行该行代码之前显示这两个值.我可能还会输入 $col_l 来显示我们正在查看的数组元素.

鉴于 您之前的问题 以及您仍在使用 C 风格的 for 循环这一事实,您已被建议不要使用,我很确定这是由于您结束了数组.

<块引用>

在 substr 中使用 $add

<块引用>

字符串外的substr

第一个警告很奇怪.对我来说,这看起来不像是 Perl 警告.你编辑的太多了吗?但第二个是明确的.您正在尝试获取一个子字符串,但使用的值超出了源字符串的长度.同样,您可以通过打印您在代码中使用的值来对此进行调查.

值得指出的是,有一个 perldiag 手册页,其中包含所有 Perl 的错误和警告以及对其含义的更详细解释.您可以通过在代码中添加 use diagnostics 来在运行时显示这些说明- 但请在问题解决后将其删除.

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

Warning 1:

Use of uninitialized value in numeric lt (<) 

Code part 2;

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

Warning 2

Use of $add in substr
substr outside of string 

解决方案

It's really not hard to diagnose either of these yourself.

Use of uninitialized value in numeric lt (<)

You know the piece of code that is giving this warning:

if ($col_lns[$col_l] < $coln)

So either $col_lns[$col_l] or $coln contains undef when you're executing this line. If I were unable to work out what the problem is, I would add a print statement that displayed those two values just before executing that line of code. I'd probably also throw in $col_l to show which element of the array we're looking at.

Given the confusion in your previous question and the fact that you're still using the C-style for loop that you've been advised against, I'd be pretty sure that this is caused by you going off the end of your array.

Use of $add in substr

substr outside of string

The first warning there is strange. It doesn't look like a Perl warning to me. Have you edited it too much? But the second is clear. You're trying to take a substring, but using values that fall outside of the length of your source string. Once again, you can investigate this by printing the values that you're using in your code.

It might be worth pointing out that there is a perldiag manual page which contains all of Perl's errors and warnings alongside more detailed explanations of what they mean. You can get those explanations displayed at runtime by adding use diagnostics to your code - but please remove it once the problems are fixed.

这篇关于如何在 Perl 中删除这些警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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