I wrote a small piece of codes that does not run as I expected:
And the output of an actual test run:
My first question is --
How can I get rid the warning?
Secondly, once I get rid of the warning, here is what I expect:
BTW, I know 'if(!$arr[$i])' would get rid of the warning, bug I don't know how distinguish '0' and 'undef'.
Thanks for the explanation and I hope I have made myself clear.
Code:
my $x1 = undef;
my $x2 = 0;
my $x3 = -1;
my $x4 = 1;
my @arr = ($x1, $x2, $x3, $x4);
for(my $i = 0; $i <= $#arr; $i++) {
if($arr[$i]) {
print "\$i = $i, \$arr[$i] = $arr[$i]\n";
}
else {
if($arr[$i] == 0) { [b]# This is line 16[/b]
print "\$i = $i, \$arr[$i] is zero\n";
}
elsif($arr[$i] < 0) {
print "\$i = $i, \$arr[$i] is negative\n";
}
else {
print "\$i = $i, \$arr[$i] = undef\n";
}
}
}
And the output of an actual test run:
Code:
% test.pl -w
Use of uninitialized value within @arr in numeric eq (==) at ./test.pl [b]line 16[/b].
$i = 0, $arr[0] is zero
$i = 1, $arr[1] is zero
$i = 2, $arr[2] = -1 // I don't understand why $arr[2] falls into the 'if' block, instead of the 'else' block
$i = 3, $arr[3] = 1
My first question is --
How can I get rid the warning?
Secondly, once I get rid of the warning, here is what I expect:
Code:
% test.pl -w
Use of uninitialized value within @arr in numeric eq (==) at ./test.pl [b]line 16[/b].
$i = 0, $arr[0] = undef
$i = 1, $arr[1] is zero
$i = 2, $arr[2] is negative // question: wouldn't 'if($arr[2])' returns false when $arr[2] == -1? At least it's case in C/C++, right?
$i = 3, $arr[3] = 1
BTW, I know 'if(!$arr[$i])' would get rid of the warning, bug I don't know how distinguish '0' and 'undef'.
Thanks for the explanation and I hope I have made myself clear.