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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.