Re: "+" and "-" are numbers.
Re: "+" and "-" are numbers.
- Subject: Re: "+" and "-" are numbers.
- From: Paul Skinner <email@hidden>
- Date: Sun, 4 Aug 2002 11:29:30 -0400
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.
set x to "-.0002AB123CDEFdsg2.2..2....kaSabc1.0E+9de
fui7yE38.9-57ehwt704.599.8675-460jfdU()*7rQEIOW5
HJ8+90Q790sdeE-8TR9WE-5UTGEJ1234E5678909VG90SD3895UJ3K"
NumbersOfText(x)
-->{-2.0E-4, 123, 2.2, 0.2, 1.0E+9, 7, 38.9, -57, 704.599, 0.8675, -460,
7, 5, 8, 90, 790, -8, 9, -5, 1234, 5678909, 90, 3895, 3}
--But as Nigel pointed out, this kind of function is next to useless on
most real data,
set x to "name:Paul Skinner PhoneNumber:555-1234 Address:5555 Great Oak
street Social Security:123-45-6789 IP:169.1.1.1"
NumbersOfText(x)
-->{555, -1234, 5555, 123, -45, -6789, 169.1, 0.1, 0.1}
NumbersOfText(x)
on NumbersOfText(x)
local thischar, nextChar, numrun, runlist
set nums to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
set numrun to ""
set runlist to {}
set x to characters of x
repeat with charIndex from 1 to length of x
set thischar to contents of item charIndex of x
try
set nextChar to contents of item (charIndex + 1) of x
on error
set nextChar to ""
end try
if thischar is in nums then
set numrun to numrun & thischar
else
if thischar is in {"-", "+", ".", "E"} then
if thischar is "E" then
if nextChar is in {"+", "-"} then
if numrun is not "" then
if character -1 of numrun is in nums then
set numrun to numrun & thischar
end if
end if
else
if numrun is not "" then
set numrun to numrun as number
set runlist to runlist & numrun
set numrun to ""
end if
end if
end if
if thischar is in {"-", "+"} then
if numrun is "" and nextChar is in nums or nextChar is "." then
set numrun to numrun & thischar
else
if numrun is not "" and character -1 of numrun is "E" then
set numrun to numrun & thischar
else
if numrun is not "" then
set numrun to numrun as number
set runlist to runlist & numrun
set numrun to thischar
end if
end if
end if
end if
if thischar is "." then
if numrun does not contain "." and nextChar is in nums then
set numrun to numrun & thischar
else
try
if numrun is not "" then
set numrun to numrun as number
set runlist to runlist & numrun
if nextChar is in nums then
set numrun to "."
else
set numrun to ""
end if
end if
end try
end if
end if
else
if numrun is not "" then
set numrun to numrun as number
set runlist to runlist & numrun
set numrun to ""
end if
end if
end if
end repeat
if numrun is not "" then
set numrun to numrun as number
set runlist to runlist & numrun
set numrun to thischar
end if
return runlist
end NumbersOfText
What can you do in the area of URL parsing? ;-)
<trudging>
Back to the white board... : )
</trudging>
--
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.