×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# How to handle $x = undef and$x = 0

## How to handle $x = undef and$x = 0

(OP)
I wrote a small piece of codes that does not run as I expected:

#### 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) { # This is line 16
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 line 16.
$i = 0,$arr is zero
$i = 1,$arr is zero
$i = 2,$arr = -1 // I don't understand why $arr falls into the 'if' block, instead of the 'else' block$i = 3, $arr = 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 line 16.$i = 0, $arr = undef$i = 1, $arr is zero$i = 2, $arr is negative // question: wouldn't 'if($arr)' returns false when $arr == -1? At least it's case in C/C++, right?$i = 3, $arr = 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. ### RE: How to handle$x = undef and $x = 0 (OP) I also tried this: #### 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] &&$arr[$i] != 0) { # line 16 print "\$i = $i, \$arr[$i] = undef\n"; } elsif(!$arr[$i] &&$arr[$i] == 0) { # line 19 print "\$i = $i, \$arr[$i] is zero\n"; } elsif($arr[$i] < 0) { print "\$i = $i, \$arr[$i] is negative\n"; } } }  And the output: #### CODE % ./test.pl -w Use of uninitialized value within @arr in numeric ne (!=) at ./test.pl line 16. Use of uninitialized value within @arr in numeric eq (==) at ./test.pl line 19.$i = 0, $arr is zero$i = 1, $arr is zero$i = 2, $arr = -1$i = 3, $arr = 1  Thanks again for your time and help!! ### RE: How to handle$x = undef and $x = 0 Hi I think that is a pure logical issue : you should check definedness first, not last : #### CODE --> ( fragment ) for(my$i = 0; $i <=$#arr; $i++) { if($arr[$i]) { print "\$i = $i, \$arr[$i] =$arr[$i]\n"; } else { unless (defined($arr[$i])) { print "\$i = $i, \$arr[$i] = undef\n"; } elsif ($arr[$i] == 0) { print "\$i = $i, \$arr[$i] is zero\n"; } elsif ($arr[$i] < 0) { print "\$i = $i, \$arr[$i] is negative\n"; } } }  ( Personally I would remove the outer if and put its block as else for that if/elsif/.../else instruction. ) To just get rid of the warning without reorganizing the conditional instruction, just prepend the test expression with the definedness test : if (defined$arr[$i] &&$arr[$i] == 0). Regarding the -1, there is nothing special with it. 0 is false, anything else is true. In C/C++, JavaScript, Awk, etc. too. #### CODE --> command line master # cat bool.c #include <stdio.h> void main(void) { int i; for (i = -2; i <= 2; i++) printf("%2d is %s\n", i, i ? "true" : "false"); } master # cc -o bool bool.c master # ./bool -2 is true -1 is true 0 is false 1 is true 2 is true  Feherke. feherke.github.io ### RE: How to handle$x = undef and \$x = 0

(OP)
Thank you, Feherke! You are the man!!

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!