Nested if statement inside a while loop.

G

gstewart

Can some perl god, please highlight the error of my ways?

For some reason the print statement below always returns true, assuming
the first check is valid.


while (<FILE>){
if (/$check/){
my @DETAIL = split(/,/,$_);
my @OTHERDETAIL = split(/:/,$DETAIL[2]);
if ($DHCPDETAIL[3]=="somevalue"){
print "You should only see me after above if statement evaluated";
}
}

It's driving me crazy. Many, many thanks.
 
J

John W. Krahn

Can some perl god, please highlight the error of my ways?

For some reason the print statement below always returns true,

Unless you close the STDOUT filehandle then yes, print() will return 'true',
but that is probably not your problem as you are not testing the return value
from print().

assuming the first check is valid.


while (<FILE>){
if (/$check/){
my @DETAIL = split(/,/,$_);
my @OTHERDETAIL = split(/:/,$DETAIL[2]);
if ($DHCPDETAIL[3]=="somevalue"){

You are using a numerical comparison on a string. Perhaps you meant:

if ( $DHCPDETAIL[ 3 ] eq "somevalue" ) {

print "You should only see me after above if statement evaluated";
}
}


John
 
G

gstewart

Ok, I'm retarded. I see the error of my ways!

It should be:

if ($DHCPDETAIL[3] eq "somevalue"){
etc...

Sorry for the spam.....
 
G

gstewart

Thanks John, you're right. It was a complete newbie mistake. My
apologies, it's late in the day and I'm tired!!!! :)
 
G

gstewart

Thanks John, you're right. It was a complete newbie mistake. My
apologies, it's late in the day and I'm tired!!!! :)
 
T

Tad McClellan

Can some perl god, please highlight the error of my ways?


A deity is hardly required, a _machine_ could have shown you the
error, but you did not ask it to.

if ($DHCPDETAIL[3]=="somevalue"){


You should always enable warnings when developing Perl code!

Argument "somevalue" isn't numeric in numeric eq (==) at ...

It's driving me crazy.


Program without

use warnings;
use strict;

and you can expect a good deal more mental anguish.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top