Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Proper use of "EQ" or while loops

by rynorx (Novice)
on Jul 22, 2013 at 15:00 UTC ( [id://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.

Replies are listed 'Best First'.
Re: Proper use of "EQ" or while loops
by MidLifeXis (Monsignor) 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 davido (Cardinal) 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 toolic (Bishop) 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 cursion (Pilgrim) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found