ne not working?

Discussion in 'Perl Misc' started by Jay Sun Ex, May 25, 2004.

  1. Jay Sun Ex

    Jay Sun Ex Guest

    I searched thru the posts for something on ne and didn't come across
    anything that could explain what my script is not doing. I am just
    doing a simple test. If I change the "ne" to "eq" and run the program
    with the same inputs it works fine, change them back to "ne" and it
    doesn't work. The source is below. I am hoping it is something
    insanely complicated so that I don't feel retarded. It should also be
    mentioned that if I take out the || and the second inequality, ne does
    what it is supposed to. I also tried substituting "or" for "||" and
    have the same results. Thanks.

    #!/usr/local/bin/perl -w

    print "\nPlease enter your name: ";
    chomp ($name = <STDIN>);
    if ($name ne "me" || $name ne "myself") {
    die "\nYou're not allowed here $name. Bye!\n";
    } else {
    print "\nWelcome $name\n";
    }
     
    Jay Sun Ex, May 25, 2004
    #1
    1. Advertising

  2. Jay Sun Ex

    J. Gleixner Guest

    Jay Sun Ex wrote:
    > I searched thru the posts for something on ne and didn't come across
    > anything that could explain what my script is not doing. I am just
    > doing a simple test. If I change the "ne" to "eq" and run the program
    > with the same inputs it works fine, change them back to "ne" and it
    > doesn't work. The source is below. I am hoping it is something
    > insanely complicated so that I don't feel retarded. It should also be
    > mentioned that if I take out the || and the second inequality, ne does
    > what it is supposed to. I also tried substituting "or" for "||" and
    > have the same results. Thanks.
    >
    > #!/usr/local/bin/perl -w
    >
    > print "\nPlease enter your name: ";
    > chomp ($name = <STDIN>);
    > if ($name ne "me" || $name ne "myself") {
    > die "\nYou're not allowed here $name. Bye!\n";
    > } else {
    > print "\nWelcome $name\n";
    > }


    If you enter 'me', then the second or (||) is true, it's not 'myself'
    ('me' ne 'myself'). The or operator defines 0 or 1 as 1.

    Get a little more aquainted with logical operators:

    perldoc perlop

    You probably want the "and" (&&) operator.

    if ($name ne 'me' && $name ne 'myself') {
    print "it's not 'me' or 'myself', it could be 'I'\n";
    }
     
    J. Gleixner, May 25, 2004
    #2
    1. Advertising

  3. Jay Sun Ex

    Paul Lalli Guest

    On Tue, 25 May 2004, Jay Sun Ex wrote:

    > I searched thru the posts for something on ne and didn't come across
    > anything that could explain what my script is not doing. I am just
    > doing a simple test. If I change the "ne" to "eq" and run the program
    > with the same inputs it works fine, change them back to "ne" and it
    > doesn't work. The source is below. I am hoping it is something
    > insanely complicated so that I don't feel retarded. It should also be
    > mentioned that if I take out the || and the second inequality, ne does
    > what it is supposed to. I also tried substituting "or" for "||" and
    > have the same results. Thanks.
    >
    > #!/usr/local/bin/perl -w
    >
    > print "\nPlease enter your name: ";
    > chomp ($name = <STDIN>);
    > if ($name ne "me" || $name ne "myself") {
    > die "\nYou're not allowed here $name. Bye!\n";
    > } else {
    > print "\nWelcome $name\n";
    > }


    You need a refresher course on first order logic. Specifically, the bit
    about only one part of a Disjunction (ie, an 'or') needing to be true for
    the whole thing to be true.

    Case 1: $name = 'me'
    $name ne "me" ==> false
    $name ne "myself" ==> true
    therefore, ($name ne "me" || $name ne "myself") ==> true

    Case 2: $name = 'myself'
    $name ne "me" ==> true
    $name ne "myself" ==> false
    therefore, ($name ne "me" || $name ne "myself") ==> true

    Case 3: $name = 'foobar'
    $name ne "me" ==> true
    $name ne "myself" ==> true
    therefore, ($name ne "me" || $name ne "myself") ==> true

    There is no case which would allow your condition to be false.

    You want && instead of ||

    Paul Lalli
     
    Paul Lalli, May 25, 2004
    #3
  4. Jay Sun Ex

    Thomas Kratz Guest

    Jay Sun Ex wrote:

    > I searched thru the posts for something on ne and didn't come across
    > anything that could explain what my script is not doing. I am just
    > doing a simple test. If I change the "ne" to "eq" and run the program
    > with the same inputs it works fine, change them back to "ne" and it
    > doesn't work. The source is below. I am hoping it is something
    > insanely complicated so that I don't feel retarded. It should also be
    > mentioned that if I take out the || and the second inequality, ne does
    > what it is supposed to. I also tried substituting "or" for "||" and
    > have the same results. Thanks.
    >
    > #!/usr/local/bin/perl -w
    >
    > print "\nPlease enter your name: ";
    > chomp ($name = <STDIN>);
    > if ($name ne "me" || $name ne "myself") {
    > die "\nYou're not allowed here $name. Bye!\n";
    > } else {
    > print "\nWelcome $name\n";
    > }


    In addition to the logic problem others pointed out, it is better to use a
    lookup hash for this kind of work:

    use strict;
    use warnings;

    my %allowed = map { $_ => undef } qw/
    me myself
    /;

    print "\nPlease enter your name: ";
    chomp ($name = <STDIN>);

    if ( exists(allowed{$name}) ) {
    print "\nWelcome $name\n";
    } else {
    die "\nYou're not allowed here $name. Bye!\n";
    }

    Thomas

    --
    open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
    STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
    print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
    &&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..
     
    Thomas Kratz, May 25, 2004
    #4
  5. Jay Sun Ex

    Matt Garrish Guest

    "Thomas Kratz" <> wrote in message
    news:...
    > Jay Sun Ex wrote:
    >
    > > #!/usr/local/bin/perl -w
    > >
    > > print "\nPlease enter your name: ";
    > > chomp ($name = <STDIN>);
    > > if ($name ne "me" || $name ne "myself") {
    > > die "\nYou're not allowed here $name. Bye!\n";
    > > } else {
    > > print "\nWelcome $name\n";
    > > }

    >
    > In addition to the logic problem others pointed out, it is better to use a
    > lookup hash for this kind of work:
    >


    If you're going to recommend strictures and warnings, your code should
    adhere to them you know... : )

    > use strict;
    > use warnings;
    >
    > my %allowed = map { $_ => undef } qw/
    > me myself
    > /;
    >
    > print "\nPlease enter your name: ";
    > chomp ($name = <STDIN>);


    chomp (my $name = <STDIN>);

    >
    > if ( exists(allowed{$name}) ) {


    if ( exists($allowed{$name}) ) {

    > print "\nWelcome $name\n";
    > } else {
    > die "\nYou're not allowed here $name. Bye!\n";
    > }
    >


    Matt
     
    Matt Garrish, May 26, 2004
    #5
  6. Jay Sun Ex

    Thomas Kratz Guest

    Matt Garrish wrote:

    > "Thomas Kratz" <> wrote in message
    > news:...
    >
    >>Jay Sun Ex wrote:
    >>
    >>
    >>>#!/usr/local/bin/perl -w
    >>>
    >>>print "\nPlease enter your name: ";
    >>>chomp ($name = <STDIN>);
    >>>if ($name ne "me" || $name ne "myself") {
    >>> die "\nYou're not allowed here $name. Bye!\n";
    >>>} else {
    >>> print "\nWelcome $name\n";
    >>>}

    >>
    >>In addition to the logic problem others pointed out, it is better to use a
    >>lookup hash for this kind of work:
    >>

    >
    >
    > If you're going to recommend strictures and warnings, your code should
    > adhere to them you know... : )


    Ooops, forgot to add 'untested' above the code ;-)
    I should better run the code I am going to post...

    [snipped code and corrections]

    Thomas

    --
    open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
    STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
    print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
    &&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..
     
    Thomas Kratz, May 26, 2004
    #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. Ratman
    Replies:
    0
    Views:
    712
    Ratman
    Sep 14, 2004
  2. Martin Heuckeroth
    Replies:
    5
    Views:
    721
    JiangZemin
    Apr 1, 2005
  3. Alan Silver
    Replies:
    1
    Views:
    3,813
    Alan Silver
    Aug 2, 2005
  4. gaurav tyagi
    Replies:
    14
    Views:
    1,374
    gaurav tyagi
    Jan 20, 2006
  5. Priyanka AGARWAL
    Replies:
    9
    Views:
    10,100
    Gordon Beaton
    May 25, 2004
Loading...

Share This Page