gpt4 book ai didi

perl - 如何在 Perl 中解决此警告

转载 作者:行者123 更新时间:2023-12-05 08:30:23 25 4
gpt4 key购买 nike

我问过这类问题 previously但没有提供完整的代码。

我正在阅读下面的文件并检查每列中存在的最大字宽,然后以适当的对齐方式将其写入另一个文件。

id0 id1 id2 batch
0 34 56 70
2 3647 58 72 566
4 39 616 75 98 78 78987 9876 7899 776
89 40 62 76
8 42 64 78
34 455 544 565

我的代码:

unlink "temp1.log";
use warnings;
use strict;
use feature 'say';
my $log1_file = "log1.log";
my $temp1 = "temp1.log";
open(IN1, "<$log1_file" ) or die "Could not open file $log1_file: $!";
my @col_lens;

while (my $line = <IN1>) {
my @fs = split " ", $line;
my @rows = @fs ;
@col_lens = map (length, @rows) if $.==1;
for my $col_idx (0..$#rows) {
my $col_len = length $rows[$col_idx];
if ($col_lens[$col_idx] < $col_len) {
$col_lens[$col_idx] = $col_len;
}
};
};
close IN1;
open(IN1, "<$log1_file" ) or die "Could not open file $log1_file: $!";
open(tempp1,"+>>$temp1") or die "Could not open file $temp1: $!";
while (my $line = <IN1>) {
my @fs = split " ", $line;
my @az;
for my $h (0..$#fs) {
my $len = length $fs[$h];
my $blk_len = $col_lens[$h]+1;
my $right = $blk_len - $len;
$az[$h] = (" ") . $fs[$h] . ( " " x $right );
}
say tempp1 (join "|",@az);
};

我的警告

Use of uninitialized value in numeric lt (<) at new.pl line 25, <IN1> line 3.
Use of uninitialized value in numeric lt (<) at new.pl line 25, <IN1> line 4.
Use of uninitialized value in numeric lt (<) at new.pl line 25, <IN1> line 4.
Use of uninitialized value in numeric lt (<) at new.pl line 25, <IN1> line 4.
Use of uninitialized value in numeric lt (<) at new.pl line 25, <IN1> line 4.
Use of uninitialized value in numeric lt (<) at new.pl line 25, <IN1> line 4.

我正在正确获取输出,但不知道如何删除此警告。

最佳答案

$col_idx 最多可以是一行中的字段数减一。对于第三行,这超过了@col_lens的最高索引,它最多包含3个元素。所以做以下是没有意义的:

if ($col_lens[$col_idx] < $col_len) {
$col_lens[$col_idx] = $col_len;
}

替换为

if (!defined($col_lens[$col_idx]) || $col_lens[$col_idx] < $col_len) {
$col_lens[$col_idx] = $col_len;
}

有了这个,检查 $ 就真的没有意义了。 == 1 了。

关于perl - 如何在 Perl 中解决此警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64660378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com