Re: Check if it is a number
Re: Check if it is a number
- Subject: Re: Check if it is a number
- From: Nigel Garvey <email@hidden>
- Date: Tue, 3 Dec 2002 13:41:58 +0000
"Arthur J. Knapp" wrote on Mon, 02 Dec 2002 10:50:25 -0500:
>
> Date: Sat, 30 Nov 2002 11:46:01 -0500
>
> Subject: Re: Check if it is a number
>
> From: Paul Skinner <email@hidden>
>
>
> NumeralsOnly("12A4B5")-->false
>
>
> on NumeralsOnly(n)
>
> try
>
> n as number
>
> return n does not contain "-" and n does not contain "+" and n does
>
> not contain " " and n is not ""
>
> on error
>
> return false
>
> end try
>
> end NumeralsOnly
>
>
This is clever, Paul, good stuff. :)
Agreed. :-) It needs one more test if it's to exclude the decimal point
character - but then it may not be meant to.
A few minor speed points, if anyone's interested.
1) Applescript appears (up to version 1.6, at least) to test 'not'
conditions by testing the positive conditions and then notting the
results. That is:
n does not contain "-"
... is performed as:
n contains "-"
not the result
I reach this conclusion from the fact that it takes slightly longer to
test if something's not so than to test if it is so. So if you have a
whole row of 'nots', as in the above handler, you can get a slight
*overall* speed improvement by changing 'and' to 'or' (or vica versa) and
gathering the 'nots' into one operation at the end:
return not (n contains "-" or n contains "+" or n contains " " or n is
"")
The advantage is greater the more tests that are actually carried out.
2) Even with an empty string, it's faster to test its length than its
identity, so:
return not ([blah] or [blah] or [blah] or (count n) is 0)
3) The above notwithstanding, if testing for a decimal point isn't
important, here's another fast variation on Paul's handler:
on NumeralsOnly(n)
try
n as number
return (count n) = (count n's first word)
on error
return false
end try
end NumeralsOnly
NG
_______________________________________________
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.