Why does ne always work

Discussion in 'Perl Misc' started by Gary Mayor, Dec 9, 2003.

  1. Gary Mayor

    Gary Mayor Guest

    Hi,
    Whilst i'm here i might as well see if anyone else has noticed strange
    things with if ($blah ne $blah) sometimes it works sometimes it doesn't.
    I always end up doing it a different way as i start pulling my hair out
    because everything is correct it just doesn't work.

    Anyone else had that problem.

    Gary
     
    Gary Mayor, Dec 9, 2003
    #1
    1. Advertising

  2. Gary Mayor

    Gary Mayor Guest

    Re: Why does ne not always work i meant

    Gary Mayor wrote:
    > Hi,
    > Whilst i'm here i might as well see if anyone else has noticed strange
    > things with if ($blah ne $blah) sometimes it works sometimes it doesn't.
    > I always end up doing it a different way as i start pulling my hair out
    > because everything is correct it just doesn't work.
    >
    > Anyone else had that problem.
    >
    > Gary
    >
     
    Gary Mayor, Dec 9, 2003
    #2
    1. Advertising

  3. Gary Mayor

    Anno Siegel Guest

    Gary Mayor <> wrote in comp.lang.perl.misc:
    > Hi,
    > Whilst i'm here i might as well see if anyone else has noticed strange
    > things with if ($blah ne $blah) sometimes it works sometimes it doesn't.


    Would you please explain what you mean by "doesn't work". "$blah ne
    $blah" will always return false. It may also issue a warning if
    $blah is undefined.

    > I always end up doing it a different way as i start pulling my hair out
    > because everything is correct it just doesn't work.
    >
    > Anyone else had that problem.


    What problem?

    Anno
     
    Anno Siegel, Dec 9, 2003
    #3
  4. Gary Mayor

    Gary Mayor Guest

    That's what i mean $blah ne $blah does not always return false. I must
    say that $blah ne $blah1 is a better description I wish I still had some
    of the code that gave me these errors but i have come accross it a
    couple of times. Say i've got this,
    $blah = "123";
    $blah1 = "1234";

    if ($blah ne $blah1) {
    print "They are not equal";
    }

    I know this works but sometimes when the values are different it doesn't
    always reconise they are not equal.

    This message is probably useless without a real example. When I come
    accross it again i'll post it up. I was just wondering if anyone else
    had seen this problem.

    Gary


    Anno Siegel wrote:
    > Gary Mayor <> wrote in comp.lang.perl.misc:
    >
    >>Hi,
    >>Whilst i'm here i might as well see if anyone else has noticed strange
    >>things with if ($blah ne $blah) sometimes it works sometimes it doesn't.

    >
    >
    > Would you please explain what you mean by "doesn't work". "$blah ne
    > $blah" will always return false. It may also issue a warning if
    > $blah is undefined.
    >
    >
    >>I always end up doing it a different way as i start pulling my hair out
    >>because everything is correct it just doesn't work.
    >>
    >>Anyone else had that problem.

    >
    >
    > What problem?
    >
    > Anno
     
    Gary Mayor, Dec 9, 2003
    #4
  5. Gary Mayor

    Anno Siegel Guest

    [reply put into sequence. please don't top-post]

    Gary Mayor <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    > > Gary Mayor <> wrote in comp.lang.perl.misc:
    > >
    > >>Hi,
    > >>Whilst i'm here i might as well see if anyone else has noticed strange
    > >>things with if ($blah ne $blah) sometimes it works sometimes it doesn't.

    > >
    > >
    > > Would you please explain what you mean by "doesn't work". "$blah ne
    > > $blah" will always return false. It may also issue a warning if
    > > $blah is undefined.
    > >
    > >
    > >>I always end up doing it a different way as i start pulling my hair out
    > >>because everything is correct it just doesn't work.
    > >>
    > >>Anyone else had that problem.


    > That's what i mean $blah ne $blah does not always return false. I must
    > say that $blah ne $blah1 is a better description I wish I still had some
    > of the code that gave me these errors but i have come accross it a
    > couple of times. Say i've got this,
    > $blah = "123";
    > $blah1 = "1234";
    >
    > if ($blah ne $blah1) {
    > print "They are not equal";
    > }
    >
    > I know this works but sometimes when the values are different it doesn't
    > always reconise they are not equal.
    >
    > This message is probably useless without a real example. When I come
    > accross it again i'll post it up. I was just wondering if anyone else
    > had seen this problem.


    I think you are confusing "ne" with "!=". If you compare two different
    strings *numerically*, the result may be that they're equal because
    they represent the same number, as in "003" and "3". String comparison
    (with "eq", "ne" and friends) always compares strings "as-is".

    There *may* be weird effects with overloaded objects, but that is
    in another category. These effects have nothing to do with comparison,
    but the strings you are comparing may not be the ones you think you
    are comparing.

    Anno
     
    Anno Siegel, Dec 9, 2003
    #5
  6. [Would you please stop top-posting?]
    [Would you please stop blindly full-quoting?]

    Gary Mayor wrote:
    > That's what i mean $blah ne $blah does not always return false. I must
    > say that $blah ne $blah1 is a better description I wish I still had


    Well, there is quite a difference between comparing the same variable and
    comparing two different variables, don't you think? Would have been nice not
    to misslead everyone :-(

    > some of the code that gave me these errors but i have come accross it
    > a couple of times. Say i've got this,
    > $blah = "123";
    > $blah1 = "1234";
    >
    > if ($blah ne $blah1) {
    > print "They are not equal";
    > }
    >
    > I know this works


    Please define "works".

    > but sometimes when the values are different it
    > doesn't always reconise they are not equal.


    Ah, finally a statement with some meat on it.
    So you are claiming that the operator 'ne' yields the wrong result in some
    circumstances. Is that correct?

    Well, you know, may I quote from
    http://www.catb.org/~esr/faqs/smart-questions.html:
    <quote>
    Q:. My program doesn't work. I think system facility X is broken.

    A:. While it is possible that you are the first person to notice an
    obvious deficiency in system calls and libraries heavily used by hundreds or
    thousands of people, it is rather more likely that you are utterly clueless.
    Extraordinary claims require extraordinary evidence; when you make a claim
    like this one, you must back it up with clear and exhaustive documentation
    of the failure case.

    </quote>

    > This message is probably useless without a real example.


    I would agree!

    > When I come
    > accross it again i'll post it up. I was just wondering if anyone else
    > had seen this problem.


    A wild guess:
    In your example $blah and $blah1 are numbers, but you elected to denote and
    compare them as strings.
    If that is the style in which you usually write your programs, then I wonder
    if maybe you ran into a 'perldoc -q 999' problem.

    jue
     
    Jürgen Exner, Dec 9, 2003
    #6
  7. In article <br4hu3$mbq$-Berlin.DE>,
    Anno Siegel <-berlin.de> wrote:
    >Gary Mayor <> wrote in comp.lang.perl.misc:
    >> > Gary Mayor <> wrote in comp.lang.perl.misc:
    >> >
    >> >>Hi,
    >> >>Whilst i'm here i might as well see if anyone else has noticed strange
    >> >>things with if ($blah ne $blah) sometimes it works sometimes it doesn't.

    >>
    >> This message is probably useless without a real example. When I come
    >> accross it again i'll post it up. I was just wondering if anyone else
    >> had seen this problem.

    >
    >I think you are confusing "ne" with "!=". If you compare two different
    >strings *numerically*, the result may be that they're equal because
    >they represent the same number, as in "003" and "3". String comparison
    >(with "eq", "ne" and friends) always compares strings "as-is".


    Another possibility is that one of the strings has leading or
    trailing whitespace. A common cause of this is forgetting to
    chomp() a line read from a file, so that the string read from
    the file still has the newline at the end, but the string it
    is being compared to has no newline.

    If I get strings that do not compare when I think they should,
    my first step is to do something like

    if ($blah ne $blah1) {
    print "OOPS -- not equal '$blah' and '$blah1'\n";
    }

    Having the single quotes there helps to see any unexpected
    spaces or newlines. You may prefer something more visible, like
    ">>>$blah<<<".

    Yes, a real example would definitely help us help you.

    Gary Ansok
    --
    Practice random acts of intelligence and senseless acts of self-control.
     
    Gary E. Ansok, Dec 9, 2003
    #7
  8. Gary Mayor

    Tintin Guest

    "Gary Mayor" <> wrote in message
    news:br4f7d$q6s$...
    > Hi,
    > Whilst i'm here i might as well see if anyone else has noticed strange
    > things with if ($blah ne $blah) sometimes it works sometimes it doesn't.
    > I always end up doing it a different way as i start pulling my hair out
    > because everything is correct it just doesn't work.
    >
    > Anyone else had that problem.


    I'm going to take a stab in the dark here and suggest you are comparing
    values from a file without chomping it.
     
    Tintin, Dec 10, 2003
    #8
  9. Gary Mayor

    Anno Siegel Guest

    Gary E. Ansok <> wrote in comp.lang.perl.misc:
    > In article <br4hu3$mbq$-Berlin.DE>,
    > Anno Siegel <-berlin.de> wrote:
    > >Gary Mayor <> wrote in comp.lang.perl.misc:
    > >> > Gary Mayor <> wrote in comp.lang.perl.misc:
    > >> >
    > >> >>Hi,
    > >> >>Whilst i'm here i might as well see if anyone else has noticed strange
    > >> >>things with if ($blah ne $blah) sometimes it works sometimes it doesn't.
    > >>
    > >> This message is probably useless without a real example. When I come
    > >> accross it again i'll post it up. I was just wondering if anyone else
    > >> had seen this problem.

    > >
    > >I think you are confusing "ne" with "!=". If you compare two different
    > >strings *numerically*, the result may be that they're equal because
    > >they represent the same number, as in "003" and "3". String comparison
    > >(with "eq", "ne" and friends) always compares strings "as-is".

    >
    > Another possibility is that one of the strings has leading or
    > trailing whitespace.


    There is only one string in "$blah ne $blah".

    [snip]

    Anno
     
    Anno Siegel, Dec 10, 2003
    #9
    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. =?ISO-8859-1?Q?Roland_M=FCller?=
    Replies:
    1
    Views:
    1,629
    =?ISO-8859-1?Q?Roland_M=FCller?=
    Nov 21, 2005
  2. Hal Vaughan
    Replies:
    11
    Views:
    1,131
    Gordon Beaton
    May 22, 2006
  3. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    465
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    914
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,013
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page