How to remove these warnings in Perl? [closed]

*爱你&永不变心* 提交于 2021-01-01 07:11:28

问题


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 


回答1:


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.



来源:https://stackoverflow.com/questions/64589416/how-to-remove-these-warnings-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!