Input error handling

Discussion in 'Perl Misc' started by MJS, Sep 24, 2003.

  1. MJS

    MJS Guest

    After the execution of the following, the only value I need in
    "number" should be any positive interger(i.e any whole numbers without
    zero).

    print "Please enter a natural number = ";
    chomp( my $number = <STDIN> );

    I need to throw an exception or inform the user when input is anything
    (characters, real number, negative number etc.) other the positve
    integer. Please help.
    MJS, Sep 24, 2003
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Purl Gurl <> wrote in news:3F7138A7.C306DB17
    @purlgurl.net:

    > MJS wrote:
    >
    > (snipped)
    >
    >> After the execution of the following, the only value I need in
    >> "number" should be any positive interger(i.e any whole numbers without
    >> zero).

    >

    ....
    > Your parameters are, paraphrased:
    >
    > Any whole number which does NOT include a zero.
    >
    > if ($number =~ tr/1-9// != length ($number))
    > { print "FUBAR"; }
    >

    (the above modified to include your followup correction)

    First, the number 109 is a positive integer, and one which the OP
    presumably wants to allow, yet it fails your test.

    Second, what an odd way of checking that a number contains only digits.
    Count the digits, and check to see whether that count is equal to the
    length of the number. Why not simply look for non-digit characters in
    the string?

    if ($number !~ /\D/ && $number != 0)
    { print "It's good!" }

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBP3Fy6GPeouIeTNHoEQIn6gCfVe+m8nF+5lBspooP2fb5xUowmlQAoOdK
    vbAJ8j9ImiQyUVOBUABh0H2r
    =cUU8
    -----END PGP SIGNATURE-----
    Eric J. Roode, Sep 24, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam
    Replies:
    12
    Views:
    652
    Steve Holden
    Oct 8, 2006
  2. cyberco
    Replies:
    6
    Views:
    690
    John Machin
    Nov 20, 2006
  3. Dietrich Bollmann
    Replies:
    0
    Views:
    353
    Dietrich Bollmann
    Apr 22, 2008
  4. Mark Tarver
    Replies:
    22
    Views:
    1,299
    J Kenneth King
    Apr 26, 2009
  5. Peter
    Replies:
    34
    Views:
    1,934
    James Kanze
    Oct 17, 2009
Loading...

Share This Page