Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Perl Beginner

by Rasha (Initiate)
on Nov 26, 2016 at 01:26 UTC ( [id://1176572]=perlquestion: print w/replies, xml ) Need Help??

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

Would you be so kind to let me know what's the mistake in this code? Thank you so much in advance! :)

print "please enter one character: "; $char = <>; while ($char ne ".") { if ($char eq "a") { $count++; } print "please enter one character: "; $char = <>; } print "The number of a is $count"; print "done";

Replies are listed 'Best First'.
Re: Perl Beginner
by Marshall (Canon) on Nov 26, 2016 at 02:25 UTC
    You are reading a line from the terminal, not just a single character. You need chomp to remove the line ending character(s). Also, get in the habit of indenting your code one level when looping.
    print "please enter one character: "; $char = <>; chomp $char; while ($char ne ".") { if ($char eq "a") { $count++; } print "please enter one character: "; $char = <>; chomp $char; } print "The number of a is $count\n"; print "done";
Re: Perl Beginner
by haukex (Archbishop) on Nov 26, 2016 at 09:53 UTC

    Hi Rasha,

    You've already gotten an answer, but in case you want to debug this issue or similar ones in the future yourself, have a look at the Basic debugging checklist, in this case especially items 3 and 4 on that list. Also, I find Data::Dumper's Useqq option helpful:

    use warnings; use strict; use Data::Dumper; $Data::Dumper::Useqq = 1; print "Enter something: "; my $char = <>; print Dumper($char);

    If I enter "3", this prints $VAR1 = "3\n";, which shows you there's a newline character (\n) at the end of the string, which you need to chomp off.

    For getting user input, there are modules that can help you, such as the core module Term::ReadLine, and there are several good modules on CPAN. Or, even though this is a little overkill, there's the prompt function from the module ExtUtils::MakeMaker - that isn't a user input module, it has a whole different function, but its prompt function comes in handy, it's a core module that should almost always be available, and it supports a default input.

    use Term::ReadLine; my $term = Term::ReadLine->new; my $line = $term->readline("Enter something: "); print Dumper($line); use ExtUtils::MakeMaker 'prompt'; my $value = prompt("Enter something:","default"); print Dumper($value);

    For reading single keypresses, there's Term::ReadKey, but that has the disadvantage that it will also return keypresses like F1 or backspace.

    use Term::ReadKey qw/ReadMode ReadKey/; print "Press a key\n"; ReadMode 'cbreak'; my $key = ReadKey(0); ReadMode 'restore'; print Dumper($key);

    Lastly, as a beginner, beginning each script with use warnings; use strict; use diagnostics; should be very helpful to you - see also Use strict and warnings.

    Hope this helps,
    -- Hauke D

Re: Perl Beginner
by tybalt89 (Monsignor) on Nov 26, 2016 at 01:34 UTC
    $char = <>;

    Sets $char to a linefeed terminated string.

Re: Perl Beginner ( print "please enter one character: "; $char = <>; )
by Anonymous Monk on Nov 26, 2016 at 01:34 UTC

    First, can you describe what you observe to be the problem with the code (it does what when you want it to do what)?

Re: Perl Beginner (reading single keystrokes)
by LanX (Saint) on Nov 26, 2016 at 21:18 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1176572]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-23 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found