Loading ...
Sorry, an error occurred while loading the content.

RE: [PBML] How to check if the user input is number?

Expand Messages
  • Stern, Leon
    You are right! I forgot all about the is... functions. ... From: Charles K. Clarkson [mailto:cclarkson@htcomp.net] Sent: Wednesday, April 02, 2003 15:52 To:
    Message 1 of 11 , Apr 2, 2003
    • 0 Attachment
      You are right! I forgot all about the is... functions.

      -----Original Message-----
      From: Charles K. Clarkson [mailto:cclarkson@...]
      Sent: Wednesday, April 02, 2003 15:52
      To: perl-beginner@yahoogroups.com
      Subject: RE: [PBML] How to check if the user input is number?



      use POSIX 'isdigit';

      foreach my $value ( 999, 'a3a', '3aa', '3 3' ) {
      print isdigit( $value )
      ? "$value is a number\n"
      : "$value is not a number\n";
      }


      HTH,

      Charles K. Clarkson
      --
      Head Bottle Washer,
      Clarkson Energy Homes, Inc.
      Mobile Home Specialists
      254 968-8328



      Yahoo! Groups Sponsor

      ADVERTISEMENT
      <http://rd.yahoo.com/M=245454.3115308.4434529.1728375/D=egroupweb/S=1705006951:HM/A=1457554/R=0/*http://ipunda.com/clk/beibunmaisuiyuiwabei>
      <http://us.adserver.yahoo.com/l?M=245454.3115308.4434529.1728375/D=egroupmail/S=:HM/A=1457554/rand=294281671>

      Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html

      Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service <http://docs.yahoo.com/info/terms/> .




      [Non-text portions of this message have been removed]
    • Charles K. Clarkson
      ... What you need is a loop that will continue to query the user after incorrect input. This loop runs forever. It is dangerous but can be escaped on most
      Message 2 of 11 , Apr 2, 2003
      • 0 Attachment
        Jaco <jaco2001uk@...> asked:
        : The question is how could the regexp check to see if the
        : value belonged to all possible values that $i takes ???


        What you need is a loop that will continue to query
        the user after incorrect input. This loop runs forever.
        It is dangerous but can be escaped on most systems.

        my $max_table = 25;
        my $input;
        while (1) {
        print "Which table (1 - $max_table)?";
        chomp( $input = <STDIN> );
        }

        To break out we use the "last" function:

        use POSIX 'isdigit';

        my $max_table = 25;

        my $input;
        while (1) {
        print "Which table (1 - $max_table)?";
        chomp( $input = <STDIN> );

        last if isdigit( $input )
        && $input > 0
        && $input <= $max_table;
        }

        print "\n\nTable selected: $input times table\n";


        HTH,

        Charles K. Clarkson
        --
        Head Bottle Washer,
        Clarkson Energy Homes, Inc.
        Mobile Home Specialists
        254 968-8328

        P.S.



        A more generic approach would allow for a range
        of answers and a user query. Using a subroutine we
        could provide a return value. Of course, this is
        probably overkill, but:

        sub query_user {
        my $range = shift;
        my $query = shift;

        # create a hash of valid responses
        my %valid;
        @valid{ @$range } = (1) x @$range;

        while (1) {
        print $query;
        chomp( my $response = <STDIN> );
        return $response if $valid{ lc $response };
        print "\nPlease try again.\n";
        }
        }

        The advantage is the flexibility.

        my $times_table =
        query_user(
        [ 1 .. 25 ],
        'Which table (1 - $max_table)?'
        );

        my $another table =
        query_user(
        [ 'yes', 'y', 'no', 'n' ],
        'Another table?'
        );

        The disadvantage is only a finite number of
        values are possible. You can't ask for any number
        between 1 and 2, for example.
      Your message has been successfully submitted and would be delivered to recipients shortly.