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
|