下面的腳本是在學習Perl的鍛鍊4.6.4。要求在不使用reverse
的情況下打印出「反向列表」。爲什麼會出現「未初始化值」的警告?
雖然產量的問題問什麼,我得到了輸入和輸出,它說「Use of unitialized value in print at line 18, <> line 4
」之間的警告。 我想我權利line 10
數組。爲什麼我仍然收到警告?
1 #!/usr/bin/perl
2 #exercise4_6_4
3 use warnings;
4 use strict;
5
6 print "Type in your list: \n";
7 my $input =<>;
8 chomp $input;
9 my $i=0;
10 my @array;
11 while ($input ne "") {
12 $array[$i] = $input;
13 $input =<>;
14 chomp $input;
15 $i++;
16 };
17 while ($i !=0) {
18 print $array[$i],"\n";
19 $i--;
20 };
21 print "$array[$i]";
運行腳本顯示以下內容:
Type in your list:
child
books
flight
Use of uninitialized value in print at exercise4_6_4.pl line 18, <> line 4.
flight
books
child
此外,以供將來參考...這可能是張貼代碼,而行是個好主意號碼在這裏。 :) – summea 2013-03-19 23:44:08
「未初始化的值」並不意味着一個未聲明的標識符,這意味着'undef'。這不是在抱怨'@ array' - 陣列永遠是「初始化」 - 而是關於'$陣列[$ i]'。 – ruakh 2013-03-19 23:44:31