Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Perl-Sensitive Sunglasses
 
PerlMonks  

Proper use of "EQ" or while loops

by rynorx (Novice)
on Jul 22, 2013 at 15:00 UTC ( #1045668=perlquestion: print w/ replies, xml ) Need Help??
rynorx has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, I've just recently started using Perl and am having trouble with my first program:

#!/usr/bin/perl print "Press C to continue\n"; $getc = <STDIN>; while ($getc ne 'c') {print "getc is $getc"; $getc = <STDIN>;} print "It worked, finally";

No matter what input I give for $getc, I'm not able to get past the while loop. Thanks in advance for any help.

Comment on Proper use of "EQ" or while loops
Download Code
Re: Proper use of "EQ" or while loops
by MidLifeXis (Prior) on Jul 22, 2013 at 15:01 UTC

    See the documentation for chomp.

    --MidLifeXis

      I got it to work with chomp. Thanks for the help.

Re: Proper use of "EQ" or while loops
by toolic (Chancellor) on Jul 22, 2013 at 15:12 UTC
    I've just recently started using Perl and am having trouble with my first program

    This may help: the Basic debugging checklist

    And you probably want to say (lower-case "c"):

    print "Press c to continue\n";
Re: Proper use of "EQ" or while loops
by davido (Archbishop) on Jul 22, 2013 at 15:30 UTC

    { my $input = ''; while( $input ne 'c' && $input ne 'C' ) { print "Press C to continue\n"; $input = <STDIN>; chomp $input; } } print "It worked, of course.\n";

    I would probably turn the Boolean expression into $input !~ /^c$/i, but your class probably hasn't gotten to regexes yet.

    If this is not homework, prompting made easy:

    use IO::Prompt::Hooked; my $input = prompt( message => 'Press c to continue.', validate => qr/^c$/i, ); print "It worked, of course.\n";

    ...of if you never need to look at $input again:

    use IO::Prompt::Hooked; prompt( message => 'Press c to continue.', validate => qr/^c$/i, ); print "If we're here, we know it worked.\n";

    Dave

Re: Proper use of "EQ" or while loops
by cursion (Scribe) on Jul 22, 2013 at 15:35 UTC
    It's helpful to put markers around things you print out for debugging or testing ...
    print "getc is [$getc]\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1045668]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2014-04-17 07:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (440 votes), past polls