in reply to Re: compare stdin for a number
in thread compare stdin for a number
This is a good start ;) you might want to add use warnings; as well#!/usr/bin/perl use strict;
This looks like it has been though word :(Print “enter a 4 digit number:\n”;
Next bit looks ok but personally I would use a while (1) last loop.print "enter a 4 digit number:\n";
Next line has been nobbled by word again but there are also a couple of other issues.Test: { my $number = <STDIN>; chomp $number;
!=~ should be !~ there was a thread the other day covering exactly what !=~ does but don't go there it is bizzare.If $number !=~ /[0-9][0-9][0-9][0-9]/{
Please use a text editor rather than word which alters what you write.if $number !~ /^[0-9][0-9][0-9][0-9]$/ {
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 getprint "try again – 4 digits\n";
Should read :-Redo TEST;
No errors here.redo Test;
So reassembled :-} }
This works and does the job. A very good start. Just please don't use word ;)#!/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; } }
Hope it helps
UnderMine
|
|---|