A hard challenge anyone?
Kay C Lan
lan.kc.macmail at gmail.com
Thu Feb 15 05:28:13 EST 2007
Been gone for a bit so I'm sorry I'm a little late and you probably have
your answer, but it was too good to resist so here's my take. My script
works on the principle that from the last 'space','(' or ')' to the next '('
must be a function name.
The script simply moves through all the chars and records the char position
of each space,( or ). The tricky bit was putting this into a useful matrix
so that it could extract the function names. Item 1 is where the last space,
( or ) was encountered. Item 2 is where the ( was encountered and item 3 is
where the ) was. It was then a simple matter of using these to extract the
function name.
As you can see to your 'test case' I added a few extra nested () that
weren't functions just to test. These just come out as multiple () in the
result so in the last line I just filter them out.
The result is like this:
aFunction()
bFunction()
cFunction()
dFunction()
eFunction()
fFunction()
In the repeat loop you'd use it in, lines 1 and 2 would be outside the loop,
whilst lines 3 to 26 would be in your loop.
Script Below.
Beware of line wraps.
There are 26 lines of code, each line starts with it's corresponding line
number.
---------
1 put 1 into tMyFunctionOpen
2 put 1 into tMyFunctionClose
3 put " put aFunction(bFunction(1)) + cFunction( (2+3), dFunction(5 +
eFunction(fFunction((a/56)+2))))" into tMyData
4 put the number of characters of tMyData into tMyCharCount
5 REPEAT with tMyChar = 1 to tMyCharCount
6 SWITCH
7 CASE (char tMyChar of tMyData = "(")
8 put (tMySpaceStore + 1) into item 1 of line tMyFunctionOpen of
tMyFunctionStore
9 put tMyChar into item 2 of line tMyFunctionOpen of tMyFunctionStore
10 add 1 to tMyFunctionOpen
11 put tMyChar into tMySpaceStore
12 break
13 CASE (char tMyChar of tMyData = ")")
14 put tMyChar into item 3 of line tMyFunctionClose of tMyFunctionStore
15 add 1 to tMyFunctionClose
16 put tMyChar into tMySpaceStore
17 break
18 CASE (char tMyChar of tMyData = " ")
19 put tMyChar into tMySpaceStore
20 break
21 END SWITCH
22 END REPEAT
23 REPEAT for each line tMyLine in tMyFunctionStore
24 put char (item 1 of tMyLine) to (item 2 of tMyLine) of tMyData & ")" &
return after tMyFunctionResult
25 END REPEAT
26 filter tMyFunctionResult without "()"
---------------
I'll now sit back and wait for the flood of examples of where this fails;-)
PS Why can't I use:
put the number of char of ....
I get an error, I have to use 'characters'. The docs indicate I can use
'the number of char'
Doesn't work in my script or in the msg box:-(
More information about the use-livecode
mailing list