in reply to vallidating a regular expression

I read that you're trying to validate that you've received a two-digit number, less than or equal to ten. That means to me you're looking for 01, 02, 03, 04, 05, 06, 07, 08, 09, and 10, and that you want to REJECT 1, 2, 3, 4, 5, 6, 7, 8 and 9. In other words, it appears by the way you've worded your question, and by the way that you've constructed your regular expression, that the leading '0' is significant to you.

Perl, on the other hand, is happy to convert strings to numbers (if it can), and numbers to strings. That's helpful sometimes, and other times it might just get in your way. Here's the issue: Let's say you start with a string, "01", and then you perform some operation on it whence Perl must treat that string as a number. That's fine, but once it's been treated as a number, Perl no longer cares that it at one time had a leading zero. This is because, numerically, 01 is the same as 1, and thus, once treated as a number, that leading zero loses significance and *poof*, it's just gone.

Now, we don't see in your example code where $totalNumber comes from. And we don't see how you've treated it within your script. My suspician is that though you may at one point have had yourself a nice "01" string, you performed some operation that resulted in the string being taken as a number, so the leading '0' disappears.

If, on the other hand, you don't care about that leading '0', your regexp should probably just be written like this:

m/^\d{1,2}$/

Also, the text your script prints errantly reports that with an input of 10 (ten), "the number is less than 10". It should say, "the number is less than or equal to ten\n".


Dave

Replies are listed 'Best First'.
Re^2: vallidating a regular expression
by ww (Archbishop) on May 11, 2006 at 14:03 UTC
    Good points from davido
     
    and,
    <demi-hemi-semi-joke> does "-10" or "1.2" also qualify? After all, neither the minus nor the decimal point is a "number." </demi-hemi...>

    and <serious point>: the OP's exact language definitely countenances any 2-digit number (where "number"'s meaning is roughly eq "value") equal to or less than 10. But it does NOT rule out the possibility that the data may contain three digit numbers, such as "007" nor single-digit numbers, like "4;" negative values such as "-10" (<BTW>Does the sign in a signed decimal count as a "digit' for OP's intended purpose?) or -- for that matter -- (hex) 0x0a (which, last time I checked) is less than 0x10. Indeed the discussion would have quite different parameters had OP used "2 digit numeral" ( ?? which would mean unicode ?!) or "two numeral value" instead of "number."</point>

    So, <peroation>1. gentle readers, perhaps the imprecision in OP's post and the wisdom in some of the replies (notably, that of reasonablekeith, who says he is neither and that cited above) will remind all who visit here, that snippet samples of data and desired outcomes can help those who would help you.

    1. Devil's Dictionary