PDA

View Full Version : Simple script inside Eval strings



ErosOlmi
15-07-2008, 00:18
Maybe not all of you know about the possibility of Eval module to evaluate little scripts more complex than a math or string expressions.
Here a little dummy script just to give you an idea.

The script check if a string variable has a "0" inside it. If yes reports it and parse 3rd token.


uses "EVAL"
uses "CONSOLE"

'---Some timing variable
dim t0, t1 as double = timer

'---This string containing the script to be executed
dim MyString as string = "
'---DIM AUTO will automatically dimension new variables when found
dim AUTO
'---No need to DIM the variable if DIM AUTO is in place
'---If variable ends with a $ it is string, otherwise numeric (EXT)
Info_GotIt$ = "" <--- Got it.""

'---Add a "GOT IT" message every time "0" is found inside the main string
IF instr(InternalScriptStringVariable, ""0"") then
InternalScriptStringVariable = InternalScriptStringVariable & Info_GotIt$
InternalScriptStringVariable = InternalScriptStringVariable & "" Number is: "" & parse$(InternalScriptStringVariable, chr$(32), 3)
END IF
"
'---Used to get back results from Eval
dim sResult as string

'---Define a looper and its max
dim Count as long
dim MaxCount as long value 500

for Count = 1 to MaxCount

'---Set InternalScriptStringVariable variable and value
Eval_SetString("InternalScriptStringVariable", "Looper is " & Count)

'---Eval_String will evaluate MyString and will return result
Eval(MyString)

'---Get back the value of the internal variable after script has modified it.
sResult = Eval_GetString("InternalScriptStringVariable")

'---Write some info output
printl format$(Count, "0000") & " - Eval_String returned: " & sResult

next

'---Measure the ending time
t1 = timer

'---Final results
printl "------------------------------------------------------"
printl "Total execution time for " & MaxCount & " loops: " & format$(t1-t0, "#0.00000")

'---Stop execution
waitkey

Petr Schreiber
15-07-2008, 09:16
And you say this "by the way" :D

That is pretty cool!
I presume "little script" is defined as script using just CORE keywords and no functions?


Thanks,
Petr

kryton9
16-07-2008, 00:52
Thanks for the demo Eros. There are lots of gems like this that are unknown I know to me. Posts like this and articles in the new journal will hope to bring these to light!

ErosOlmi
16-07-2008, 06:35
I presume "little script" is defined as script using just CORE keywords and no functions?
Not exacly but very close.
EVAL is an interpreter inside the main thinBasic interpreter but supported functionalities in EVAL are not the same as in Core in the sense that even if keywords in EVAL use the same syntax as the Core, it is not the same engine.

EVAL supports very few set of keywords but anyhow quite sufficient to write little scripts.
I will update EVAL help file. I need time >:( :D