我收到了运行我的Perl脚本之一的警告。我正在测试一个简单的if
语句,我正在测试一个数组中的字符串是否与另一个字符串相同。字符串'eq'中未初始化的值Perl
我的同事和我已经尝试了几种情况,仍然无法解决警告。我试图把我所有的研究都放到这个主题中,所以它有点长,但请坚持下去。我完全卡住了,希望堆栈的一个伟大的思想 溢出可以帮助我!
的代码产生的问题是:
if ($pieces[0] eq "PromotionNumber")
的代码围绕该部分的块是:
my @pieces = split(/=/, $var);
if($pieces[0] eq "PromotionNumber") {
$promoNumber = $pieces[1];
} elsif ($pieces[0] eq "Type") {
# More similar code follows
我在上面的代码的目标是分配所有我发现变量在一个文本文件中添加到各自的Perl变量中。然后,我将这些找到的变量插入到SQL数据库中。
该文本文件有几个字段可以以不同的顺序,这就是为什么我使用开关样式if-elsif ...来完成赋值。还有一些我不关心的领域,比如Level,我只是忽略了这些领域。但是,这些字段是导致警告的字段。
的$var
因为它遍历被设置为以下...
PromotionNumber=000
RecordOffset=0
Code=0
SubCode=1
Level=0
当我打“级别= 0”,我可以在PerlIDE.exe调试器暂停并看到该字符串分割进入Level和0并插入到数组中。但是,只要代码前进到if
声明并测试$pieces[0] eq "PromotionNumber"
,我就会收到警告。
我甚至可以在if
声明之前打印$ pieces [0],它将打印出“Level”。
如果我的代码更改为警告消失以下...
my @pieces = split(/=/, $var);
if($pieces[0] eq "Level") {
#My problematic variable test
}elsif($pieces[0] eq "PromotionNumber") {
$promoNumber = $pieces[1];
} elsif ($pieces[0] eq "Type") {
#More similar code follows
但是,如果我测试了“级别”的字符串第二,警告回来。下面的代码有警告。
my @pieces = split(/=/, $var);
if($pieces[0] eq "PromotionNumber") {
#My problematic variable test
}elsif($pieces[0] eq "Level") {
$promoNumber = $pieces[1];
} elsif ($pieces[0] eq "Type") {
#More similar code follows
为什么Perl关心我测试的顺序?请注意,我正在测试几个其他字符串,这些字符串在我的if-elsif语句中是多个elsif的,并没有给出这个警告。
任何想法?我真的需要清除这个警告,以便在运行时不会溢出控制台。该脚本正在处理警告。
精确的错误是:在japdpmrijob.pl路线错误的250
确切的行(使用PerlIDE Perl的调试工具来确定字符串EQ
使用未初始化值。exe)是:
if ($pieces[0] eq "PromotionNumber") {
我可以打印出$ pieces [0]并查看值。所以我知道它是用我的价值定义的。我还可以打印出$件[1]并查看我的预期值。如果我先测试$ pieces [0] eq“Level”,警告消失,我可以访问这两个变量。
我仍然困惑...
它看起来像错误实际上是“EQ”被标记为一个变量。有关于此的任何想法?
下面你会发现一大块代码。我包括了整个
for
循环以及我正在使用的几个变量。注意if-elsif-else序列末尾的else
声明,我添加了这个以尝试停止第三个答案所指出的警告。这个else
语句会在每次调用警告时打印我的预期值,所以我知道这些值是存在的。for my $cond (@conditions) { if($debug==1){print $cond."\n";} # Required database variables my $isRecord = 0; my $promoNumber; my $type; my $process; my $testValue; my $recordOffset; my $code; my $subcode; my $itemType; my $itemValue; # Function test variables my $itemTypeVar; my $newQualifier = 1; # Database Configuration my $dbApps = new Win32::ODBC("myDatabase") || die "Error: " . Win32::ODBC::Error(); my @condVars = split(/\|/, $cond); for my $var (@condVars) { if($debug==1){print $var."\n";} my @pieces = split(/=/, $var); if(defined($pieces[0])){ print "piece 0 defined!\n"; } else { print "pieces 0 not defined!\n"; } if(defined($pieces[1])){ print "piece 1 defined!\n"; } else { print "piece 1 not defined!\n"; } if($pieces[0] eq "PromotionNumber"){ $promoNumber = $pieces[1]; } elsif ($pieces[0] eq "Type"){ $type = $pieces[1]; } elsif ($pieces[0] eq "Process"){ $process = $pieces[1]; } elsif ($pieces[0] eq "TestValue"){ $testValue = $pieces[1]; } elsif ($pieces[0] eq "RecordOffset"){ $recordOffset = $pieces[1]; if ($recordOffset == 0) { $newQualifier = 1; } } elsif ($pieces[0] eq "Code"){ $code = $pieces[1]; } elsif ($pieces[0] eq "SubCode"){ $subcode = $pieces[1]; } elsif ($pieces[0] eq "ItemType"){ $itemType = $pieces[1]; if($itemType eq "0") { $itemTypeVar = "ItemCode"; } elsif($itemType eq "1") { $itemTypeVar = "ItemCode"; } elsif($itemType eq "2") { $itemTypeVar = "Department"; } elsif($itemType eq "5") { $itemTypeVar = "MixMatchCode"; } elsif($itemType eq "12") { $itemTypeVar = "GroupCode"; } } elsif ($pieces[0] eq $itemTypeVar){ $itemValue = $pieces[1]; } else { print "$pieces[0] and $pieces[1] not used.\n"; } print "$var\n"; } }
几乎可以肯定的问题是'$ pieces [1]'表达式,而不是'$ pieces [0]'。如果你有一个没有'='的'$ var'的值(例如一个空行)'split'将返回一个单一项目的列表。您对“类型”和“级别”的测试仅查看第一个元素并且可以工作,但“PromotionNumber”下的块试图查看不存在的第二个元素并生成警告。 –
请发布您正在获取的确切错误。目前还不清楚是哪条线路导致问题。 – chepner
另外,是否有可能向我们展示更多的代码(至少是设置'$ var'的值的for循环)。这是一个非常奇怪的问题。 – chepner