CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
|| operators not working?

 



droidus2
Novice

Nov 13, 2015, 2:46 PM

Post #1 of 4 (1766 views)
|| operators not working? Can't Post


Code
#!/usr/bin/perl -w  
use strict;
use warnings;

print "\nWould you like to select another folder(yes/no): ";
my $UI = <>;
$UI = cleanUI($UI);
while($UI ne 'y' || $UI ne 'yes')
{
print "\nBad Input($UI) - Would you like to select another folder(yes/no): ";
$UI = <>;
$UI = cleanUI($UI);
}

sub cleanUI
{
my $ui = shift;
chomp($ui);
$ui =~ s/^\s+|\s+$//g;
return $ui;
}


When I run this, and hit 'y', it tells me bad input. when I remove "|| $UI ne 'yes'", it does work though. How can I get this to work?


Chris Charley
User

Nov 13, 2015, 3:44 PM

Post #2 of 4 (1762 views)
Re: [droidus2] || operators not working? [In reply to] Can't Post

The answer is DeMorgan's Laws

NOT y OR NOT yes by his law is equivalent to NOT(y AND yes). This is what you have now. The right expression is NOT y AND NOT yes (which equals NOT(y OR yes) ).


Laurent_R
Veteran / Moderator

Nov 14, 2015, 4:06 AM

Post #3 of 4 (1747 views)
Re: [droidus2] || operators not working? [In reply to] Can't Post

Chris is entirely correct, but just to clarify in another way, you might think of it this way.

If you think seriously about it:

Code
($UI ne 'y' || $UI ne 'yes')

will ALWAYS be true.

Suppose the user entered "y", then $UI ne 'y' will be false, so that $UI ne 'yes' will be tested and return true (since "y" is not equal to "yes").

If the user entered "yes", then $UI ne 'y' will return true ("yes" is no equal to "y") and $UI ne 'yes' will not even be tested (but even it if were tested, the full expression would still return true).

And if the user entered anything else, then again $UI ne 'y' will return true (and $UI ne 'yes' will not even be tested but even it if were tested, the full expression would still return true).

So you need:

Code
($UI ne 'y' and $UI ne 'yes')

or possibly:

Code
(not ($UI eq 'y' or $UI eq 'yes'))



(This post was edited by Laurent_R on Nov 14, 2015, 4:08 AM)


FishMonger
Veteran / Moderator

Nov 14, 2015, 6:15 AM

Post #4 of 4 (1741 views)
Re: [droidus2] || operators not working? [In reply to] Can't Post

Others have already explained why it was not working and one possible way to fix it. Personally, I'd use a regex because IMO it's cleaner and shorter.


Code
while($UI !~ /^y(es)?$/i)


However, that still leaves you with another problem. We're still not testing for the other valid input. A small adjustment to the regex will fix that issue as well.


Code
while($UI !~ /^y(es)?$|^no?$/i)



(This post was edited by FishMonger on Nov 14, 2015, 6:17 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives