Re: Check if it is a number
Re: Check if it is a number
- Subject: Re: Check if it is a number
- From: Paul Skinner <email@hidden>
- Date: Thu, 12 Dec 2002 17:10:18 -0500
On Friday, December 6, 2002, at 08:15 AM, has wrote:
Paul Skinner wrote:
I thought that the code I posted on Sat Nov 30, 2002 was nice because
you could read it and easily understand it's logic.
Indeed. But that doesn't mean it wasn't full of holes.
snip
After double-checking the test code to make sure it wasn't at fault
(always a wise precaution), I worked through your handler's code till
I figured out the problem: the 'n contains "-"' test is suceptible to
the effects of an external 'ignoring hyphens' block [1].
Right, that's a hole.
To avoid the possibility of your handler giving bad results, these
comparisons should be wrapped in a 'considering hyphens' block.
on NumeralsOnly(n)
try
n as number
considering hyphens
return (get not ((count of n) = 0 or n contains "-" or n
contains "+"))
end considering
on error
return false
end try
end NumeralsOnly
Thanks! that's even better.
I should clarify that I think that it's unlikely in the OP's case that
he would wrap this handler call with a 'ignoring hyphens' block unless
he wanted that result. I intended to answer his specific request, not
produce a hardened handler.
[2] After making a couple of fixes
Any changes other than adding the considering hyphens block?
I've got it passing the following tests, working on the assumption
that only positive integers or reals are allowed [2a]:
property validVals : {"1", "512", "001", "0.01", "0.0001", "324.21",
"1000000000", "0.000000000001"}
property invalidVals : {"", " ", {}, {1}, true, 1, "+", "1-", "1.1.1",
"one", "-1", "1.0E+5", "1.0E-5"}
Which isn't to say I haven't overlooked some really gnarly values to
throw at the thing, but at least it's looking a bit more promising.
[2a] 'NumeralsOnly' is much too vague; the handler name should be
changed to something like 'isPositiveNumber'.
At least it's not completely accurate. Since decimals are allowed. But
'isPositiveNumber' would sound like it will accept terms like '1.0E+5'
or '+5'. How about
'isUnsignedPositiveIntegerOrRealAndIsNotScientificNotation' : )
--
Paul Skinner
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.