2012-08-15 45 views
2

我写了一些代码,并且我不确定错误是什么。我得到的错误:使用未初始化的值在串联(。)或字符串mksmksmks.pl行63

Use of uninitialized value in concatenation (.) or string at mksmksmks.pl line 63

我的代码如下:

for(my $j = 0; $j < $num2; $j++) { 
    print {$out} "$destination[$j]|$IP_one_1[$j]|$IP_one_2[$j]|$reached[$j]|$IP_two_1[$j]|$IP_two_2[$j]\n";` 
} 
+0

和我行63是由for循环 – user1581917 2012-08-15 20:29:50

+7

这是不可能告诉一个哪些值未根据您向我们显示的内容初始化。使用Perl调试器或添加一些'print'语句,并检查在(非常长)字符串文字中引用的所有变量的值;其中之一是“undef”。 – 2012-08-15 20:33:22

+0

另外还有一个提示:也许最好将5个数组中的数据重组为哈希数组?我想,处理起来会更清洁。 – raina77ow 2012-08-15 20:37:47

回答

6

它的意思是,要么@destination@IP_one_1@IP_one_2,或@reached尚未定义的元素之一,或已被赋值为undef。您可能需要检测(并阻止)来源处未定义的值,或者稍后期待和处理它们。由于你有warnings启用(这是一件好事),Perl提醒你,你的代码试图连接一个字符串,其中一个值被连接的值是未定义的。

您可能会调查的一个示例是一个或多个数组的总大小与其他数组不同。例如,如果@IP_one_2的元素数少于其他元素,或者$num2的值大于任何数组中的元素数。

地点靠近你的脚本的顶部以下并再次运行它:

use diagnostics; 

当我运行下的警告和诊断下面的一行:

$ perl -wMdiagnostics -e '$a=$a; print "$a\n"' 

我得到以下输出,如果你添加use diagnostics;,你会得到类似的东西......当你第一次学习Perl的警告时,一个非常有用的工具。

Use of uninitialized value $a in concatenation (.) or string at -e line 1 (#1)

(W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.

To help you figure out what was undefined, perl will try to tell you the name of the variable (if any) that was undefined. In some cases it cannot do this, so it also tells you what operation you used the undefined value in. Note, however, that perl optimizes your program anid the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program.

-1

如果您使用eq ""它不会给出任何警告消息。

但是如果你使用eq " "(在这里你可以看到一个空格),那么它将给这个警告信息:

Use of uninitialized value in concatenation (.) or string ....