Re: "+" and "-" are numbers.
Re: "+" and "-" are numbers.
- Subject: Re: "+" and "-" are numbers.
- From: Paul Skinner <email@hidden>
- Date: Fri, 2 Aug 2002 16:33:28 -0400
On Friday, August 2, 2002, at 01:42 PM, Arthur J. Knapp wrote:
Date: Fri, 2 Aug 2002 10:54:41 -0400
Subject: "+" and "-" are numbers.
From: Paul Skinner <email@hidden>
"+" as number
-->0
"-" as number
-->0
Why?
Bug! ;-)
That's what I thought too.
It is most likely a parsing problem related to the fact that a
"number" can start with a plus or minus sign. Sometimes, this
can be useful:
SNIP number parsing handler.
P.S. If someone asked Nigel Garvey very politely, he could probably
come up with a much nicer and robust number parsing routine. :)
{ Arthur J. Knapp, of <http://www.STELLARViSIONs.com>
a r t h u r @ s t e l l a r v i s i o n s . c o m
}
I'll take a crack at it myself. But if anyone can improve on it please
do...
set x to "-.0002AB123CDEFdsg2.2..2....kaSabc1.0E+9de
fui7yE38.9-57ehwt704.5998675-0460jfdU()*7rQEIOW5
HJ8+90Q790E8TR9WEUTGEJ9VG90SD3895UJ3K"
NumbersOfText(x)
-->{
-2.0E-4,
123,
2.2,
0.2,
1.0E+9,
7,
38.9,
57,
704.5998675,
460,
7,
5,
8,
90,
7908,
9,
9,
90,
3895,
3
}
on NumbersOfText(x)
local thischar, nextChar, numrun, runlist
set nums to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
set punct to {"-", "+", ".", "E"}
set valids to nums & punct
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 --not a digit
if thischar is in punct then
if thischar is "E" and nextChar is in {"+", "-"} then
if character -1 of numrun is in nums then
set numrun to numrun & thischar
end if
end if
if thischar is in {"-", "+"} then
if numrun is "" and nextChar is in valids 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 ""
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 --second ddecimal.
try
if numrun is not "" then
set numrun to numrun as number --Close current numrun
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
return runlist
end NumbersOfText
--
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.