Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Nested if statement inside a while loop. (http://www.velocityreviews.com/forums/t894458-nested-if-statement-inside-a-while-loop.html)

gstewart@gmail.com 09-29-2005 01:18 AM

Nested if statement inside a while loop.
 
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.


John W. Krahn 09-29-2005 01:33 AM

Re: Nested if statement inside a while loop.
 
gstewart@gmail.com wrote:
> 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
--
use Perl;
program
fulfillment

gstewart@gmail.com 09-29-2005 01:36 AM

Re: Nested if statement inside a while loop.
 
Ok, I'm retarded. I see the error of my ways!

It should be:

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

Sorry for the spam.....


gstewart@gmail.com 09-29-2005 01:37 AM

Re: Nested if statement inside a while loop.
 
Thanks John, you're right. It was a complete newbie mistake. My
apologies, it's late in the day and I'm tired!!!! :-)


gstewart@gmail.com 09-29-2005 01:37 AM

Re: Nested if statement inside a while loop.
 
Thanks John, you're right. It was a complete newbie mistake. My
apologies, it's late in the day and I'm tired!!!! :-)


Tad McClellan 09-29-2005 12:09 PM

Re: Nested if statement inside a while loop.
 
gstewart@gmail.com <gstewart@gmail.com> wrote:

> 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.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas


All times are GMT. The time now is 06:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.