perl 未初始化值...错误

发布于 2024-12-11 19:45:54 字数 1386 浏览 0 评论 0原文

我在使用严格的代码中不断收到初始化错误,但不明白为什么。我为所有变量设置了正确的范围 - 并且代码运行。只是不明白这些丑陋的错误。

2/15/2002   Joe   155
2/15/2002   Mike  108
2/15/2002   Pete  209
2/22/2002   Joe   158
2/22/2002   Mike  99
2/22/2002   Pete  163
3/1/2002    Joe   172
3/1/2002    Mike  125
#!/usr/bin/perl -w
our %dates;
foreach my $line (<>) {
    chomp $line;
    my ($this_date, $this_name, $this_score) = split /\s+/, $line;
    my ($record_name, $record_score) = split /\|/, $dates{$this_date};
    if ($this_name && $this_score) {
            if ($this_score > $record_score) {
                    $dates{$this_date} = join "|", ($this_name, $this_score);
            }
    }
}

foreach my $date (keys %dates) {
    my ($name, $score ) = split /\|/, $dates{$date};
    print " The high_scored for $date was $name with $score\n";
shortcasper@shortcasper-laptop:~/perl$ ./hash_bowl bowl_linux
Use of uninitialized value in split at ./hash_bowl line 8,  line 7.
Use of uninitialized value $record_score in numeric gt (>) at ./hash_bowl line 10,    line 7.
Use of uninitialized value in split at ./hash_bowl line 8,  line 7.
Use of uninitialized value $record_score in numeric gt (>) at ./hash_bowl line 10,    line 7.
The high_scored for 3/1/2002 was Joe with 172
The high_scored for 2/15/2002 was Pete with 209
shortcasper@shortcasper-laptop:~/perl$

I keep on getting initialization errors in this code on the use strict and can't figure out why. I set the scope right for all the variables - and the codes runs. Just don't understand the ugly errors.

2/15/2002   Joe   155
2/15/2002   Mike  108
2/15/2002   Pete  209
2/22/2002   Joe   158
2/22/2002   Mike  99
2/22/2002   Pete  163
3/1/2002    Joe   172
3/1/2002    Mike  125
#!/usr/bin/perl -w
our %dates;
foreach my $line (<>) {
    chomp $line;
    my ($this_date, $this_name, $this_score) = split /\s+/, $line;
    my ($record_name, $record_score) = split /\|/, $dates{$this_date};
    if ($this_name && $this_score) {
            if ($this_score > $record_score) {
                    $dates{$this_date} = join "|", ($this_name, $this_score);
            }
    }
}

foreach my $date (keys %dates) {
    my ($name, $score ) = split /\|/, $dates{$date};
    print " The high_scored for $date was $name with $score\n";
shortcasper@shortcasper-laptop:~/perl$ ./hash_bowl bowl_linux
Use of uninitialized value in split at ./hash_bowl line 8,  line 7.
Use of uninitialized value $record_score in numeric gt (>) at ./hash_bowl line 10,    line 7.
Use of uninitialized value in split at ./hash_bowl line 8,  line 7.
Use of uninitialized value $record_score in numeric gt (>) at ./hash_bowl line 10,    line 7.
The high_scored for 3/1/2002 was Joe with 172
The high_scored for 2/15/2002 was Pete with 209
shortcasper@shortcasper-laptop:~/perl$

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

婴鹅 2024-12-18 19:45:54

您应该使用警告而不是使用-w

它抱怨的原因是,当您第一次遇到特定日期时,$dates{$this_date}undef(因为它从未被设置过)。拆分会给您一个警告,并使 $record_name$record_score undef (当您比较 $this_score 时,会导致您的第二个警告代码> 到 $record_score)。该代码之所以有效,是因为在数字上,undef 被视为 0,但它会生成警告。

一个简单的解决方法是使用 $dates{$this_date} ||改为“|0”。这为新日期提供了默认值,将 $record_name 设置为空字符串,将 $record_score 设置为 0:

use strict;
use warnings;

our %dates;
foreach my $line (<DATA>) {
    chomp $line;
    my ($this_date, $this_name, $this_score) = split /\s+/, $line;
    my ($record_name, $record_score) = split /\|/, $dates{$this_date} || '|0';
    if ($this_name && $this_score) {
            if ($this_score > $record_score) {
                    $dates{$this_date} = join "|", ($this_name, $this_score);
            }
    }
}

foreach my $date (keys %dates) {
    my ($name, $score ) = split /\|/, $dates{$date};
    print " The high_scored for $date was $name with $score\n";
}

__DATA__
2/15/2002   Joe   155
2/15/2002   Mike  108
2/15/2002   Pete  209
2/22/2002   Joe   158
2/22/2002   Mike  99
2/22/2002   Pete  163
3/1/2002    Joe   172
3/1/2002    Mike  125

但您应该阅读 Perl 数据结构指南 并考虑使用复杂的数据结构,而不必合并拆分 您的数据只是为了存储它在哈希中。

You should use warnings instead of using -w.

The reason it complains is that the first time you encounter a particular day, $dates{$this_date} is undef (because it's never been set). Splitting that gives you a warning and makes $record_name and $record_score undef (causing your second warning when you compare $this_score to $record_score). The code works because numerically, undef is considered 0, but it generates warnings.

A simple fix is to use $dates{$this_date} || '|0' instead. This provides a default value for new dates, setting $record_name to the empty string and $record_score to 0:

use strict;
use warnings;

our %dates;
foreach my $line (<DATA>) {
    chomp $line;
    my ($this_date, $this_name, $this_score) = split /\s+/, $line;
    my ($record_name, $record_score) = split /\|/, $dates{$this_date} || '|0';
    if ($this_name && $this_score) {
            if ($this_score > $record_score) {
                    $dates{$this_date} = join "|", ($this_name, $this_score);
            }
    }
}

foreach my $date (keys %dates) {
    my ($name, $score ) = split /\|/, $dates{$date};
    print " The high_scored for $date was $name with $score\n";
}

__DATA__
2/15/2002   Joe   155
2/15/2002   Mike  108
2/15/2002   Pete  209
2/22/2002   Joe   158
2/22/2002   Mike  99
2/22/2002   Pete  163
3/1/2002    Joe   172
3/1/2002    Mike  125

But you should read the Perl Data Structures Cookbook and consider using a complex data structure instead of having to join and split your data just to store it in a hash.

万劫不复 2024-12-18 19:45:54

在我看来,您的哈希 %dates 已初始化,但其值未初始化。您的代码尝试在实际设置之前使用 $dates{$this_date} 的值(这将在几行之后发生)。

It seems to me that your hash %dates is initialized but its values are not. Your code tries to use the value of $dates{$this_date} before it is actually set (which happens a few lines later).

还如梦归 2024-12-18 19:45:54

%dates 是一个空哈希。所以 $dates{$this_date} 正在变成“undef”

改变你的内在条件,如下所示,使其发挥作用;当没有旧记录时,它将插入日期哈希中。

 if ( !(defined  $record_score) || $this_score > $record_score) {
                $dates{$this_date} = join "|", ($this_name, $this_score);
        }

%dates is a empty hash. So $dates{$this_date} is becoming "undef"

Change your inner condition like below to make it work; when there is no old records, it will insert in the dates hash.

 if ( !(defined  $record_score) || $this_score > $record_score) {
                $dates{$this_date} = join "|", ($this_name, $this_score);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文