A hard challenge anyone?
Kay C Lan
lan.kc.macmail at gmail.com
Thu Feb 15 22:13:36 EST 2007
OK here is the updated script which takes into account an uneven number of (
or ) which is only likely to occur within quotes. It does this by simply NOT
doing anything whilst within quotes.
Here is the single line of code I tested it on:
put offset("(","some text ( sin(a + b)/tan(c*d)") &
afunct(bfunct(dfunct([hfunct(1)+sin(1)+tan(1)*jfunct(2)]/kfunct(3))+efunct(4)),cfunct(ffunct(5)-gfunct(6)))=afunct("(","(")
into tMyData
Note the use of function names right against tokens, to help confuse the
matter:-)
The result was:
offset()
afunct()
bfunct()
dfunct()
hfunct()
sin()
tan()
jfunct()
kfunct()
efunct()
cfunct()
ffunct()
gfunct()
afunct()
Which includes not only inbuilt and custom functions, but multiple
occurrences of functions. If it means anything they are also listed in the
order that they occur.
NOTE though, that it doesn't register the sin() and tan() functions that
occur in quotes in the offset function! Only the sind() and tan() in
hfunct() are listed. Again, how often do you feed function names within
quotes into a variable for latter use will determine how much time you want
to spend on covering this situation;-)
The following line was removed:
filter tMyFunctionResult without "()"
as taking into account quoted text seemed to remove any blank lines.
The script has been turned into a function so you'd just feed it the line of
script in a repeat loop. Obviously if the line doesn't contain any "(" it
doesn't need to be fed into this function.
Script Below
Watch For Line Breaks
50 lines of code, each starting with it's respective line number
--------
1 FUNCTION fFunctionFinder pMyData
2 put 1 into tMyFunctionOpen
3 put 1 into tMyFunctionClose
4 put "closed" into tMyQuotes
5 put the number of characters of pMyData into tMyCharCount
6 REPEAT with tMyChar = 1 to tMyCharCount
7 SWITCH
8 --needs to occur first so that you know when quotes close
9 CASE ((char tMyChar of pMyData = quote) AND (tMyQuotes = "open"))
10 put "closed" into tMyQuotes
11 break
12 --catch for opening quotes
13 CASE ((char tMyChar of pMyData = quote) AND (tMyQuotes = "closed"))
14 put "open" into tMyQuotes
15 break
16 --the following 3 CASE are irrelevant unless outside quotes
17 CASE ((char tMyChar of pMyData = "(") AND (tMyQuotes = "closed"))
18 put (tMySpaceStore + 1) into item 1 of line tMyFunctionOpen of
tMyFunctionStore
19 put tMyChar into item 2 of line tMyFunctionOpen of tMyFunctionStore
20 add 1 to tMyFunctionOpen
21 put tMyChar into tMySpaceStore
22 break
23 CASE ((char tMyChar of pMyData = ")") AND (tMyQuotes = "closed"))
24 put tMyChar into item 3 of line tMyFunctionClose of tMyFunctionStore
25 add 1 to tMyFunctionClose
26 put tMyChar into tMySpaceStore
27 break
28 CASE ((char tMyChar of pMyData = " ") AND (tMyQuotes = "closed"))
29 put tMyChar into tMySpaceStore
30 break
31 END SWITCH
32 END REPEAT
33 REPEAT for each line tMyLine in tMyFunctionStore
34 put char (item 1 of tMyLine) to (item 2 of tMyLine) of pMyData & ")" &
return after tMyFunctionResult
35 END REPEAT
36 --where function names immediately follows a token the token is included
at the front of the function name. this removes it
37 replace comma with empty in tMyFunctionResult
38 replace "=" with empty in tMyFunctionResult
39 replace "+" with empty in tMyFunctionResult
40 replace "-" with empty in tMyFunctionResult
41 replace "*" with empty in tMyFunctionResult
42 replace "/" with empty in tMyFunctionResult
43 replace "[" with empty in tMyFunctionResult
44 replace "]" with empty in tMyFunctionResult
45 replace "{" with empty in tMyFunctionResult
46 replace "}" with empty in tMyFunctionResult
47 replace "<" with empty in tMyFunctionResult
48 replace "." with empty in tMyFunctionResult
49 return tMyFunctionResult
50 END fFunctionFinder
------
HTH
PS Origininally tried to replace the tokens with the efficient:
put "=, +, -, *, /, [, ], {, }, <, >" into tMyTokens
REPEAT for each item tMyItem in tMyTokens
replace tMyItem with empty in tMyFunctionResult
END REPEAT
and even tried this as the replace line above:
replace quote & tMyItem & quote with empty in tMyFunctionResult
but neither worked. Bug?
More information about the use-livecode
mailing list