First up note that the condition (as stated) is satisfied for some values of $b that are not digits in the range 1 to 5. For example the numbers 2.0, 3e0, 4.0e0, and the string '5anything' will all satisfy the condition. If you really want to ensure that $b is a single digit in the range 1 to 5, then I think the following may suffice -given that the value comes from a form, and is therefore presumably being assigned to $b as a string, not a number:
if (length($b)== 1 && $b >= 1 && $b <= 5) { print "\$b has to be a number and is either 1, 2, 3, 4, or 5."; }
But if $b were to be assigned the number 4.0e0, rather than the string '4.0e0', you would find it still satisifed the condition (as rewritten by me).

On the other hand, if all you want to do is use the numeric value of whatever it is that the user supplied, then I can't really think of any improvement you could make on your original if() statement.

Cheers,
Rob
Update: Replaced length($b == 1) with length($b) == 1 ... smart move, methinks :-)

In reply to Re: Quicker way to do this IF statement 1-5 by syphilis
in thread Quicker way to do this IF statement 1-5 by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.