当我运行此代码时出现这些错误我得到了这些警告我真的不明白。当我将==更改为a = i时仍然会出错。 :
Use of uninitialized value in numeric eq (==) at ./pennies_again line 53, <$ogen_fh> line 2336
Use of uninitialized value in numeric eq (==) at ./pennies_again line 53, <$ogen_fh> line 2336.
Use of uninitialized value in numeric eq (==) at ./pennies_again line 53, <$ogen_fh> line 2336.
Use of uninitialized value in numeric eq (==) at ./pennies_again line 53, <$ogen_fh> line 2336.
Use of uninitialized value in numeric eq (==) at ./pennies_again line 53, <$ogen_fh> line 2336.
Use of uninitialized value in numeric eq (==) at ./pennies_again line 53, <$ogen_fh> line 2336.
Use of uninitialized value in numeric eq (==) at ./pennies_again line 53, <$ogen_fh> line 2336.
Use of uninitialized value in numeric eq (==) at ./pennies_again line 53, <$ogen_fh> line 2336.
Use of uninitialized value in numeric eq (==) at ./pennies_again line 53, <$ogen_fh> line 2336.
A
AA
AAL
AAPL
ABT
ABT
SPIN
ABX
这是猫的第2336行 - 看起来并不多 - 我跳过了这一行 - 我认为这是我真正关心的第53行。
$ cat -n /imp/data/config/foo.cfg | tail
2327 AOSAskEdge = rAOSAskEdge
2328 AOSBidEdge = rAOSBidEdge
2329 }
2330
2331
2332 SvcSwap = {
2333 VCE = TVGEN
2334 }
2335 }
2336 # vi:set cindent,cino=+0
这是代码,它是符号哈希值为symbol_hash {$ 1} == 1的部分。我甚至不知道这意味着什么:
#!/sbc/perl/bin/perl
#/sbcimp/etds_extend.pl
use strict;
use warnings;
use DBI;
use Data::Dumper;
my $dbUser = '1';
my $dbPass = '_01';
my $dbSid = 'A.WORLD';
my $dbh = DBI->connect( "dbi:Oracle:$dbSid", "$dbUser", "$dbPass" ) or die("Couldn't connect: $!");
#sub read_extend
my %symbol_hash = ();
my $query = "select e_risk_symbol from gsd_etds where level_name='CS' order by e_risk_symbol";
if ( !$dbh ) {
print "Error connecting to DataBase; $DBI::errstr\n";
}
my $cur_msg = $dbh->prepare($query) or die "\n\nCould not prepare statement: " . $dbh->errstr;
$cur_msg->execute();
while ( my @row = $cur_msg->fetchrow_array ) {
$symbol_hash{ $row[0] } = 1;
}
#for my $symbol (sort keys %symbol_hash ) {
# print "$symbol\n" ;
#
#}
my %extend_hash = ();
my @error_array = ();
my %penny_hash = ();
my $penny_file = "/sbcig/qgen.cfg";
my $ticksize_flag = 0;
my $penny_flag = 0;
my $member_flag = 0;
open( my $ogen_fh, '<', "$penny_file" ) or die "Can't open $penny_file for reading: $!";
foreach my $line (<$ogen_fh>) {
next if ( $line =~ m/vi:set cindent,cino=+0/ );
chomp($line);
if ( $line =~ /^\s*TickSize\s+=\s+\{/ ) {
$ticksize_flag = 1;
}
elsif ( ( $ticksize_flag == 1 ) && ( $line =~ /^\s*penny.*\s+=\s+\{/ ) ) {
$penny_flag = 1;
}
elsif ( ( $penny_flag == 1 ) && ( $line =~ /^\s*members\s+=\s+\{/ ) ) {
$member_flag = 1;
}
elsif ( ( $member_flag == 1 ) && ( $line =~ /\}/ ) ) {
$member_flag = 0;
}
elsif ( ( $member_flag == 1 ) && ( $line =~ /\s*(\S+)\s*$/ ) ) {
if ( $symbol_hash{$1} == 1 ) { ### Line 53
$penny_hash{$1} = 1;
}
}
elsif ( ( $penny_flag == 1 ) && ( $line =~ /\}/ ) ) {
$penny_flag = 0;
}
elsif ( ( $ticksize_flag == 1 ) && ( $line =~ /\}/ ) ) {
$ticksize_flag = 0;
}
}
for my $penny ( sort keys %penny_hash ) {
print "$penny\n";
sleep 1;
}
答案 0 :(得分:2)
%symbol_hash
的每个元素的值都是1
,因此您可以从$symbol_hash{EXPR}
得到的两个值是1
和未定义的(对于不存在的元素)。后者使用==
进行比较时发出警告。
您正在检查哈希元素是否存在,因此您可以使用
if (exists($symbol_hash{$1}))
而不是
if ($symbol_hash{$1} == 1)
但是既然所有存在的价值都是真的而那些不是假的,那么所需要的只是
if ($symbol_hash{$1})
答案 1 :(得分:1)
您的%symbol_hash
并非始终针对您尝试访问的密钥进行初始化:
if ( $symbol_hash{$1} == 1 ) { ### Line 53
一个解决方法是在数字比较==
中使用它之前验证密钥是否存在:
if ( $symbol_hash{$1} && $symbol_hash{$1} == 1 ) {
但是,考虑到哈希初始化的上下文,唯一可能的值是1
或undef
。
因此,您真正想要测试的是一个真正的值:
if ( $symbol_hash{$1} ) {