Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Nested if statement inside a while loop.

Reply
Thread Tools

Nested if statement inside a while loop.

 
 
gstewart@gmail.com
Guest
Posts: n/a
 
      09-29-2005
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.

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-29-2005
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
 
Reply With Quote
 
 
 
 
gstewart@gmail.com
Guest
Posts: n/a
 
      09-29-2005
Ok, I'm retarded. I see the error of my ways!

It should be:

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

Sorry for the spam.....

 
Reply With Quote
 
gstewart@gmail.com
Guest
Posts: n/a
 
      09-29-2005
Thanks John, you're right. It was a complete newbie mistake. My
apologies, it's late in the day and I'm tired!!!!

 
Reply With Quote
 
gstewart@gmail.com
Guest
Posts: n/a
 
      09-29-2005
Thanks John, you're right. It was a complete newbie mistake. My
apologies, it's late in the day and I'm tired!!!!

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-29-2005
<> 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
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
break inside of case- statement inside of loop Alexander Korsunsky C Programming 25 02-27-2007 05:39 AM
How to break a while loop inside a switch statement? howachen@gmail.com Java 16 07-11-2006 10:23 AM
Modifying Array inside While statement Andy Perl Misc 21 12-29-2005 11:14 PM
nested while - how to go to the beginning of the first while? invni C Programming 36 07-27-2005 09:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57