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: Tue, 3 Dec 2002 10:44:19 -0500
On Tuesday, December 3, 2002, at 08:41 AM, Nigel Garvey wrote:
"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.
Interesting, OMM (AS 1.9) these two are identically quick over 100000
iterations.
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)
Really? I'd think the cost of this would be higher.
ie. getting the value and comparing it to null vs gettting the value
and getting it's count and comparing that to 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
Much more concise than my 'naught, naught carry the naught'*, but
Arthur already found it troublesome.
* --Jethro Bodine
--
Paul Skinner
Mac OS X 10.2.2
AppleScript 1.9
OSAXen installed
ColorSyncScripting.app
Digital Hub Scripting.osax
FontSyncScripting.app
Image Capture Scripting.app
Keychain Scripting.app
StandardAdditions.osax
URL Access Scripting.app
Adobe Unit Types
24U Hex OSAX
GetMilliSec.osax
XML Tools.osax
_______________________________________________
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.