Nested if statement inside a while loop.

Discussion in 'Perl Misc' started by gstewart@gmail.com, Sep 29, 2005.

  1. Guest

    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.
     
    , Sep 29, 2005
    #1
    1. Advertising

  2. 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
     
    John W. Krahn, Sep 29, 2005
    #2
    1. Advertising

  3. Guest

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

    It should be:

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

    Sorry for the spam.....
     
    , Sep 29, 2005
    #3
  4. Guest

    Thanks John, you're right. It was a complete newbie mistake. My
    apologies, it's late in the day and I'm tired!!!! :)
     
    , Sep 29, 2005
    #4
  5. Guest

    Thanks John, you're right. It was a complete newbie mistake. My
    apologies, it's late in the day and I'm tired!!!! :)
     
    , Sep 29, 2005
    #5
  6. <> 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
     
    Tad McClellan, Sep 29, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,237
    Tim Slattery
    Mar 30, 2005
  2. Replies:
    16
    Views:
    3,603
    steve
    Jul 11, 2006
  3. Alexander Korsunsky

    break inside of case- statement inside of loop

    Alexander Korsunsky, Feb 25, 2007, in forum: C Programming
    Replies:
    25
    Views:
    1,073
    Richard Heathfield
    Feb 27, 2007
  4. Uday Bidkar
    Replies:
    4
    Views:
    493
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    381
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page