BioBoy has asked for the wisdom of the Perl Monks concerning the following question:

Hello all. Perl seems to be tripping me up on the basics.. Anyway, I have this basic while loop, which has the responsibility of checking 'STDIN' for either two specific characters. If they are not entered, the while loop asks the user for input again, until one of the characters is entered:
# - ask the user whether or not they would like to align their sequenc +es Locally or Globally print $username . ", would like to align your protein sequences gl +obally or locally? (enter either 'g' or 'l') "; $alignment_type = <STDIN>; # - debug print "\n", $alignment_type, "\n"; # - debug while( ($alignment_type ne 'g') or ($alignment_type ne 'l') ){ print $username . ", your selection was incomprehensible (ente +r either 'globally' or 'locally'): "; $alignment_type = <STDIN>; } print "\n";
Even if either 'g' or 'l' are entered, the code always gets trapped in the loop. Confused...

Replies are listed 'Best First'.
Re: Basic 'while' loop problem
by davido (Cardinal) on Oct 04, 2005 at 06:45 UTC

    You just need to chomp the user input before testing it. When the user enters 'l' or 'g', he also hits "enter", which appends a '\n' character at the end (newline). $alignment_type will never be equal to 'g' or 'l' until you've removed that newline character with chomp.

    Just remember that anytime you read from a filehandle you need to account for trailing newline characters1. chomp does that for you.

    1. Unless the record separator has been changed or you're performed some other similar trickery. (update)


    Dave

Re: Basic 'while' loop problem
by muntfish (Chaplain) on Oct 04, 2005 at 11:15 UTC

    Others have mentioned chomp which is good advice - but I also wonder if you have a logic problem.

    Your loop condition basically says "try again if input is not g, or not l". So if the user types g, "not g" is false, but "not l" is true, so it complains.

    If the condition for valid input is (l or g) then the logical negation of that is (not (l or g)) which can be written as (not l and not g).

    So - you need an and instead of an or.

    I hope that makes sense, apologies if I've misunderstood.


    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&
      yep, that was it. should have used 'and' instead of 'or'. silly me. thanks. I did use chomp, though.
Re: Basic 'while' loop problem
by tirwhan (Abbot) on Oct 04, 2005 at 09:55 UTC
    The chomp advice above is correct, but for a different approach you could use Damian Conway's IO::Prompt module instead:
    use IO::Prompt; my $query_prompt="$username would like to align your protein sequences + globally or locally? (enter either 'g' or 'l')"; my $error_prompt="$username, your selection was incomprehensible (ente +r either 'g' or 'l'):"; my $alignment_type = prompt ( -prompt => $query_prompt, -require => { $error_prompt => qr/^[gl]$ +/i }, );
Re: Basic 'while' loop problem
by PodMaster (Abbot) on Oct 04, 2005 at 06:46 UTC
    Examine the output of print "The length is ", length($alignment_type), "\n"; then use chomp.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Basic 'while' loop problem
by husker (Chaplain) on Oct 04, 2005 at 14:24 UTC
    Hello all. Perl seems to be tripping me up on the basics.

    I direct you a fine book of Perl basicness, Learning Perl, 3rd edition. This book will give you a tremendous leg up on the fundamentals of Perl. Bonus: the author hangs out here from time to time. :)

    Edit: Apparently some found this post too fawning for their tastes.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Basic 'while' loop problem
by Anonymous Monk on Oct 04, 2005 at 07:21 UTC
    Or, instead of using chomp, you can do:
    s/[\r\n]+$//;