Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hope you don't mind me making a few comments and I hope you find them helpful.
#!/usr/bin/perl use strict;
This is a good start ;) you might want to add use warnings; as well
Print “enter a 4 digit number:\n”;
This looks like it has been though word :(
Word uses odd quoting that changes the quote (") charecter and will capitalise first letters.
print "enter a 4 digit number:\n";
Next bit looks ok but personally I would use a while (1) last loop.
Test: { my $number = <STDIN>; chomp $number;
Next line has been nobbled by word again but there are also a couple of other issues.
If $number !=~ /[0-9][0-9][0-9][0-9]/{
!=~ should be !~ there was a thread the other day covering exactly what !=~ does but don't go there it is bizzare.
/[0-9][0-9][0-9][0-9]/ matches four digits true enough but it will match anything that contains 4 succesive digits (ie 'ABC12CD E3456FGH') and the original post asked for exactly 5 digits (ignoring the 4 - 5 issue ;)) you need to lock the pattern to the start (^) and end ($) of the string. /^[0-9][0-9][0-9][0-9]$/ would do this.
if $number !~ /^[0-9][0-9][0-9][0-9]$/ {
Please use a text editor rather than word which alters what you write.
Print “try again – 4 digits\n”;
De-worded this is fine.
print "try again – 4 digits\n";
There is a problem with the following line but againg it is related to case sensitivity. The label you loop back to has to match including case. Other wise you get
Label not found for "redo TEST" at - line 9, <STDIN> line 1.
Redo TEST;
Should read :-
redo Test;
No errors here.
} }
So reassembled :-
#!/usr/bin/perl use strict; print "enter a 4 digit number:\n"; Test: { my $number = <STDIN>; chomp $number; if ($number !~ /^[0-9][0-9][0-9][0-9]$/) { print "try again – 4 digits\n"; redo Test; } }
This works and does the job. A very good start. Just please don't use word ;)

Hope it helps
UnderMine


In reply to Re: Re: compare stdin for a number by UnderMine
in thread compare stdin for a number by anoxer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found