• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Check if it is a number
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Check if it is a number


  • Subject: Re: Check if it is a number
  • From: has <email@hidden>
  • Date: Thu, 5 Dec 2002 15:35:00 +0000

[Haven't been following this thread, so apologies for belly-flopping into the pool and upsetting the civil swimmers...]

Nigel Garvey wrote:

on NumeralsOnly(n)
try
n as number
return result as string is n and result >= 0
on error
return false
end try
end NumeralsOnly


Even a very quick poke at the above reveals a bunch of faulty and/or inconsistent cases and susceptibility to outside influences:

NumeralsOnly("1000000000")
NumeralsOnly("001")
NumeralsOnly("0.01")
NumeralsOnly("0.001")

NumeralsOnly("-1")
NumeralsOnly("1.0E+5")
NumeralsOnly("1.0E-5")

ignoring hyphens, punctuation and white space
NumeralsOnly("10 - 1")
NumeralsOnly("255.0.0.0 ")
NumeralsOnly("1!")

NumeralsOnly("-1")
NumeralsOnly("1.0E+5")
NumeralsOnly("1.0E-5")
end ignoring


I'd suggest the following approach:
1. work out what values will and won't be regarded as valid
2. compose a comprehensive set of test cases [1]
3. write a version that works reliably (speed be damned)
4. mess around with optimisations/clever hacks last.



(Pity AS still doesn't have regular expressions. e.g. "-?[0-9]+(\.[0-9]+)?" would test if a string was a positive or negative integer or real, while "-?[0-9]\.[0-9]+E(\+|-)[0-9]+" would test for sci notation. [2])

has

[1] Here's the test code. Couldn't be simpler. All you gotta do is think up a really good range of test values to pound your routine with (the better your tests, the less chance of bugs slipping through), then hack away to your heart's content:

property validVals : {"512", "001", "0.0001", "324.21"...}
property invalidVals : {"", {}, true, 1, "+", "1-", "1.1.1", "one"...}

ignoring hyphens, punctuation, white space, etc...
repeat with valRef in validVals
if not NumeralsOnly(valRef's contents) then error valRef's contents
end repeat
repeat with valRef in invalidVals
if NumeralsOnly(valRef's contents) then error valRef's contents
end repeat
end ignoring

return failedTests

[2] These are completely off the top of my head though, so don't quote me on this. They could also be refined if you need to be more picky about style (e.g. no leading zeros).
--
--
http://www.barple.pwp.blueyonder.co.uk -- The Little Page of AppleScripts
_______________________________________________
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.
  • Follow-Ups:
    • Re: Check if it is a number
      • From: Paul Skinner <email@hidden>
  • Prev by Date: Re: scriptable faxsoftware?
  • Next by Date: RE: scriptable faxsoftware?
  • Previous by thread: Re: Check if it is a number
  • Next by thread: Re: Check if it is a number
  • Index(es):
    • Date
    • Thread