ne not working?

J

Jay Sun Ex

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";
}
 
J

J. Gleixner

Jay said:
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";
}
 
P

Paul Lalli

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
 
T

Thomas Kratz

Jay said:
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
 
M

Matt Garrish

Thomas Kratz said:
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 = said:
if ( exists(allowed{$name}) ) {

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

Matt
 
T

Thomas Kratz

Matt said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top