Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am a beginner. I am trying to write a program where user should be able to enter only numbers, and no other keys on computer. It should throw and error or allow user to enter a number again, if he mistypes anything. Thanks in advance.

Replies are listed 'Best First'.
Re: force user to only enter numbers
by toolic (Bishop) on Sep 02, 2011 at 16:14 UTC
      perldoc -f number

      No documentation for perl function `number' found

      perldoc number
Re: force user to only enter numbers
by Marshall (Canon) on Sep 02, 2011 at 23:10 UTC
    Mastery of the basic command loop is important. This works the same in Java, C, C++, etc.

    The basic structure is some sort of a loop that stops when some "quit" or "exit" condition is satisfied. I like to use "while" loops.

    Standard reaction when a blank line is input is to just re-prompt - that is not an error.

    Standard reaction when blanks occur either before or after the input, is nothing! These extra leading or trailing blanks are not errors.

    #!/usr/bin/perl -w use strict; while ( (print "Enter a number: "), (my $number = <STDIN>) !~ /^\s*q(uit)?\s*$/i ) { next if $number =~ /^\s*$/; # re-prompt on blank line if ($number =~ /^\s*[-+]?[0-9]*\.?[0-9]+\s*$/) #a decimal floatin +g point { print "Great! number is: $number\n"; } else { print "illegal number - try again!\n"; } }
Re: force user to only enter numbers
by ww (Archbishop) on Sep 02, 2011 at 22:07 UTC

    Above, you have several good answers to turn in (this is homework, isn't it? If so, better to label it so there's no question about your integrity) but your original code suggests you'll benefit from some of the pointers in the comments below:

    #!/usr/bin/perl use strict; # strict and warnings are your friends. USE THEM AL +WAYS! use warnings; # 923876 my $num; input(); # you need not dump all your functions into subs # and certainly not into chained subs, but using ch +ained subs # here makes it easier for me to point out a few th +ings =head print "\n Press any key to exit"; # Utterly unnecessary - # your program will exit when it run +s out of $key = <STDIN>; # instructions, absent code to speci +fically do # otherwise ... such as sub quit_or_ +repeat: #End of Program =cut sub input { print "Enter an integer that is between 0 and 9: "; # no newline +so user can chomp ($num = <STDIN>); # enter data +at prompt test_input(); } sub test_input { if ( $num =~ /^[1-8]$/ ) { # 1...8 are the numbers "between 0 an +d 9" # as spec'ed by the prompt in OP's co +de main(); } else { print "Only single-digit, positive integers, 1 - 8, are allowe +d\n"; input(); } } sub main { for(my $i=1; $i<=$num; $i++) { print "$i "; } print "\n"; quit_or_repeat(); # sub called here actually does something # contrast w/ the original "$key = <STDIN> # # End of Program" + } sub quit_or_repeat { my $key = ''; print "\n Press 'q' to quit or any other key to continue: "; chomp ($key = <STDIN>); if ( lc($key) eq 'q' ) { # either 'q' or "Q" quits exit; } else { input(); # go another round } }
Re: force user to only enter numbers
by AR (Friar) on Sep 02, 2011 at 15:49 UTC

    Can you show us what you have so far?

    If you don't have anything yet, you might consider building a loop that reads from STDIN, and then checks the string for non-numeric characters with a regular expression. Don't forget to chomp! Once the string is in the format you want, leave the loop and process it however you want.

      This is what I have tried
      print "Enter a number\n"; $num = <STDIN>; if ($num < 0) { print "please enter a number that is between 0 and 9 \n"; $num = <STDIN>; } #Get input +from the keyboard for($i=1;$i<=$num;$i++) #For loop to repeat a + set of statements until a condition is met { print $i; } print "Press any key to exit"; $key = <STDIN>; #End of Program
Re: force user to only enter numbers
by pemungkah (Priest) on Sep 02, 2011 at 20:11 UTC
    There's Scalar::Util::looks_like_number(), or course. Is any number OK, or are you specifically wanting only 0 to 9? (I ask because your spec says "only numbers", but your sample code seems to be saying 0 to 9. An accurate idea of what you are planning is crucial to doing exactly what you want to do, instead of kind of doing it.)

    If you really meant 0..9, then use

    if ($number =~ /^[0-9]$/) { ...
    That says "one character, which must be 0 through 9, and no others".
Re: force user to only enter numbers
by JavaFan (Canon) on Sep 02, 2011 at 16:20 UTC
    Assuming for you, a number is a sequence of ASCII digits (untested):
    my $num; { print "Please type a number: "; chomp($num = <>); redo unless $num =~ /^[0-9]+$/; }
Re: force user to only enter numbers
by Anonymous Monk on Sep 02, 2011 at 16:09 UTC
    ... print "enter a number" my $num = <STDIN> unless ($num =~/[0-9]+/){ print "this is not a number\n please type a number" } print "your number is $num"; ...

      mmh, I forgot all ";" and this don't go

      much better like this:

      ... print "enter a number: \n"; my $num = <STDIN>; if ($num !~/^[+-]?\d+$/){ print "this is not a number\n please type a number\n"; } else {print "your number is $num\n"} ...
      Update: my $num = int(<STDIN>); thus, 12.45 now is a number

      ups... not so good idea,

      enter a number: dfr34

      your number is 0

        hohohou...

        use strict; print "enter a number: "; my $num = <STDIN>; if ($num !~/^[+-]?\d+\.?\d*$/){print "this is not a number\n"} else {print "your number is $num\n"} __OUTPUT__ 34 -> your number is 34 34.56 -> your number is 34.56 -1 -> your number is -1 34gtf -> this is not a number
        Thanks..This is what I was looking for..