Re: "+" and "-" are numbers.
Re: "+" and "-" are numbers.
- Subject: Re: "+" and "-" are numbers.
- From: Paul Skinner <email@hidden>
- Date: Mon, 5 Aug 2002 11:18:11 -0400
On Monday, August 5, 2002, at 03:22 AM, Andy Wylie wrote:
on 5/8/02 3:29 AM +1200: Paul Skinner wrote:
On Sunday, August 4, 2002, at 10:38 AM, Arthur J. Knapp wrote:
Date: Fri, 2 Aug 2002 16:33:28 -0400
Subject: Re: "+" and "-" are numbers.
From: Paul Skinner <email@hidden>
On Friday, August 2, 2002, at 01:42 PM, Arthur J. Knapp wrote:
P.S. If someone asked Nigel Garvey very politely, he could probably
come up with a much nicer and robust number parsing routine. :)
I'll take a crack at it myself. But if anyone can improve on it
please
do...
on NumbersOfText(x)
Very nice :)
<blush> ; )
Oops! I missed some signs on negative numbers following other numbers
as
well as not handling Es in number runs that were not part of an
exponential notation number.
I couldn't help myself...
set a to "-.0002AB123CDEFdsg2.2..2....kaSabc1.0E+9de
fui7yE38.9-57ehwt704.599.8675-460jfdU()*7rQEIOW5
HJ8+90Q790sdeE-8TR9WE-5UTGEJ1234E5678909VG90SD3895UJ3K"'s text items
set bag to {}
set wannabees to {".", "*"}
set f to false
considering case
repeat with n from 1 to count a
set x to a's item n
try
x as number
set end of bag to x
set f to false
on error number n
if n = -1700 then
if x is in wannabees then
set end of bag to x
else if f = false then
if x = "E" then
set end of bag to x
else
set end of bag to return
set f to true
end if
end if
end if
end try
end repeat
end considering
bag as string
_____________________________ Nigel Wannabe
-->"-.0002
123
2.2..2....
1.0E+9
7
38.9-57
704.599.8675-460
*7
5
8+90
790
-8
9
-5
1234E5678909
90
3895
3
"
Hmmm...
I'm not sure that does exactly what you wanted it to do.
Thinking about Nigel's comment as to the uselessness of such a
function, I thought that something more generic might actually be useful
now and then. This one extracts numbers and number-based data from the
given text.
Trash data can give non-number results... ie. "2.2..2...."
set inputText to
"-.0002AB123CDEFdsg2.2..2....kaSabc1.0E+9defui7yE38.9-57ehwt704.599.8675-460jfdU(
)*7rQEIOW5HJ8+90Q790sdeE-8TR9WE-5UTGEJ1234E5678909VG90SD3895UJ3K"
getNumberData(inputText)
-->{"-.0002", "123", "2.2..2....", "1.0E+9", "7", "38.9-57",
"704.599.8675-460", "7", "5", "8+90", "790", "9", "1234", "5678909",
"90", "3895", "3"}
... but real world data should be handled well.
set inputText to "name:Paul Skinner PhoneNumber:555-1234 Address:5555 Great
Oak street Social Security:123-45-6789 IP:169.1.1.1 favorite number:4.2E+
7"
getNumberData(inputText)
-->{"555-1234", "5555", "123-45-6789", "169.1.1.1", "4.2E+7"}
This handler uses the space-run removal routine from the 'Trim'
thread. It's already useful! I frequently have to remove the null
entries that are left by removing items via TIDs. This makes it trivial!
on getNumberData(inputText)
repeat with theseDelimiters in {{"E+", "E"}, {"E-", "o#?"}}
set AppleScript's text item delimiters to item 1 of theseDelimiters
set inputText to inputText's text items
set AppleScript's text item delimiters to item 2 of theseDelimiters
set inputText to inputText as text
end repeat
repeat with i from 1 to 255
set thisItem to ASCII character i
considering case
if thisItem is not in {"1", "2", "3", "4", "5", "6", "7",
"8", "9", "0", "-", "+", ".", "o#?", "E"} then
set AppleScript's text item delimiters to thisItem
set inputText to inputText's text items
set AppleScript's text item delimiters to " "
set inputText to inputText as text
end if
end considering
end repeat
repeat with theseDelimiters in {{"E", "E+"}, {"o#?", "E-"}}
set AppleScript's text item delimiters to item 1 of theseDelimiters
set inputText to inputText's text items
set AppleScript's text item delimiters to item 2 of theseDelimiters
set inputText to inputText as text
end repeat
set AppleScript's text item delimiters to " "
set inputText to text items of inputText
repeat with i from 1 to length of inputText
try
if character 1 of (item i of inputText) is "E" then set
item i of inputText to ""
end try
end repeat
set space2 to space & space
set inputText to space & inputText & space
repeat while inputText contains space2
set AppleScript's text item delimiters to space2
set tempS to text items of inputText
set AppleScript's text item delimiters to space
set inputText to tempS as text
end repeat
set AppleScript's text item delimiters to ""
set inputText to text 2 thru -2 of inputText
set AppleScript's text item delimiters to " "
set inputText to text items of inputText
end getNumberData
* "E" is opt shift q, "o#?" is opt shift k
--
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.