perl 未初始化值...错误
我在使用严格的代码中不断收到初始化错误,但不明白为什么。我为所有变量设置了正确的范围 - 并且代码运行。只是不明白这些丑陋的错误。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该
使用警告
而不是使用-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:但您应该阅读 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}
isundef
(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:But you should read the Perl Data Structures Cookbook and consider using a complex data structure instead of having to
join
andsplit
your data just to store it in a hash.在我看来,您的哈希
%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).%dates 是一个空哈希。所以 $dates{$this_date} 正在变成“undef”
改变你的内在条件,如下所示,使其发挥作用;当没有旧记录时,它将插入日期哈希中。
%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.