2012-08-15 44 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 ....