perl代码如下,为什么for循环后数组a变成了未初始化? 5
1#!/usr/bin/perl-w23@a=(1,6,3,8,2,4,9,7,0);45print"@a\n";67for(@a){8open(L,"./a.sh");...
1 #!/usr/bin/perl -w
2
3 @a=(1,6,3,8,2,4,9,7,0);
4
5 print "@a\n";
6
7 for(@a){
8 open(L,"./a.sh");
9 while(<L>){
10
11 }
12 close L;
13 }
14
15 print "@a\n";
运行结果如下:
1 6 3 8 2 4 9 7 0
Use of uninitialized value $a[0] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[1] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[2] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[3] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[4] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[5] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[6] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[7] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[8] in join or string at ./t2.pl line 15.
第5行能打印出数组a,第15行却提示a未初始化,为什么呢? 展开
2
3 @a=(1,6,3,8,2,4,9,7,0);
4
5 print "@a\n";
6
7 for(@a){
8 open(L,"./a.sh");
9 while(<L>){
10
11 }
12 close L;
13 }
14
15 print "@a\n";
运行结果如下:
1 6 3 8 2 4 9 7 0
Use of uninitialized value $a[0] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[1] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[2] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[3] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[4] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[5] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[6] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[7] in join or string at ./t2.pl line 15.
Use of uninitialized value $a[8] in join or string at ./t2.pl line 15.
第5行能打印出数组a,第15行却提示a未初始化,为什么呢? 展开
1个回答
展开全部
具体初始化方法可以参考如下程序段:(方法一)
1
int a[5] = {0}; // 将整型数组a的每个元素都初始化为0
或者也可以用如下方法进行初始化:(方法二)
1
int a[5] = {0, 0, 0, 0, 0}; // 将整型数组a的每个元素都初始化为0,初始化效果同上
方法一的初始化方法是将数组元素都初始化为同一个数值;
方法二的初始化方法可以分别对数组的每个元素进行初始化,每个元素的数值可以不同。
1
int a[5] = {0}; // 将整型数组a的每个元素都初始化为0
或者也可以用如下方法进行初始化:(方法二)
1
int a[5] = {0, 0, 0, 0, 0}; // 将整型数组a的每个元素都初始化为0,初始化效果同上
方法一的初始化方法是将数组元素都初始化为同一个数值;
方法二的初始化方法可以分别对数组的每个元素进行初始化,每个元素的数值可以不同。
追问
我问的是Perl语言哦
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询