Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: compare stdin for a number

by NovMonk (Chaplain)
on Mar 12, 2004 at 20:41 UTC ( [id://336260]=note: print w/replies, xml ) Need Help??


in reply to compare stdin for a number

Since I have nothing better to do this afternoon, and I'm just learning, too, I checked my Perl book and came up with this-- does it work? If not, why not?

#!/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; } }

(I had to look in a couple of chapters of Perl by Example to get this. At least I'm using strict now, though.)

NovMonk

Replies are listed 'Best First'.
Re: Re: compare stdin for a number
by UnderMine (Friar) on Mar 12, 2004 at 22:32 UTC
    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

Re: Re: compare stdin for a number
by neniro (Priest) on Mar 12, 2004 at 22:06 UTC
    If $number !=~ /[0-9][0-9][0-9][0-9]/{

    If you want a negation you sould use !~ as operator instead of =~ or unless instead of if as condition. The regex matches 4 digits anywhere in $number. Correct use of possible regexe's is explained above.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-19 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found