ScriptSpot

Forum topic · General Scripting

Convert string to float / array problem

By Rodman · 2012-11-01

Description

Hello,

I want to convert a string which contains a formula but once i want to use it as a float, it doesn't work.

Look at my script to understand the problem :


the_array = #()
for t = 0 to 100 do (append the_array t)

------------------------------------------------------------------------------------------------
-- replace tt in string by the_array[t]
------------------------------------------------------------------------------------------------

formula = "(" + "cos((tt*2)*(180/pi))+tt" + ")"

tokens = filterString formula "tt" splitEmptyTokens:true

for i = 1 to tokens.count do
(
if tokens[i] == "" do
(
tokens[i] = "(the_array[t])"
)
)

formula = ""

for i = 1 to tokens.count do
(
formula += tokens[i]
)
------------------------------------------------------------------------------------------------

x_matrix = bigMatrix 1 the_array.count

for t = 1 to the_array.count do
(
formula = formula as float

x_matrix[1][t] = formula -- I want to have formula = (cos((the_array[t]*2)*(180/pi))+the_array[t])
)

Comments (8)

just a fun idea, maybe...

Anubis · 2012-11-01

but try it out anyway :)

fn buildFormula val = (
	local ss = stringStream ""
	format "cos((%*2)*(180/pi))+%" val val to:ss
	local result = ss as string
	free ss; result
)


fn proxyEval &strExpr &expCntrl = (
	if expCntrl.SetExpression strExpr then
	(expCntrl.Update(); expCntrl.value)
	else 0
)

the_array = for t = 0 to 100 collect t
exc = expression_controller()
formula = ""

for t = 1 to the_array.count do
(
	formula = buildFormula t
	proxyEval &formula &exc
)

p.s. - Most scripters not like

execute()

as it evaluate in the global scope, but if that not a problem for your script, then just use

format()

to build the string, but if that formula is unknown to the script, and you know only that it holds "tt" variable, then float expresion or float script can help.

Here is with float script:

fsc = float_script()
fsc.script = "cos((tt*2)*(180/pi))+tt"
fsc.addConstant "tt" 0

arr_results = #()
arr_results[100]  = 0

for t = 1 to 100 do
(
	fsc.SetConstant "tt" &t
	fsc.Update() -- eval expr.
	arr_results[t] = fsc.value
)

I got errors

Rodman · 2012-11-01

I have errors when I execute it.
I can't understand your script. If you can tell me how it works it could help to solve the errors.

I want to use any mathematic formulas. And I think your script works for just 1.

By the way, thank you Master.

well...

Anubis · 2012-11-01

The main idea was to use controllers to evaluate your math expresion (instead of Execute() function), but if my code is too alien and can't debbug it yourself then just skip it for now and stick to using Execute().

>> "I want to use any mathematic formulas"

- Did you mean for example "((cos(X-D)/sin(Y+M))*(Z/E)^pi)+F"? Here is alot of variables.... lol :) if you made some inputbox where to type any formula, then should make as well some naming convension. For example if you use var T it should always mean Time, and so on. I think that the script controllers can helps you, as they allow to setup as much variables as you need (and with the var-names by your choice).

and...

Anubis · 2012-11-02

If i get correctly your goal, then searching for how to replace the char inside a string with float (or other type) value is a wrong direction.
Your math formula (as string or not) needs to get some value from anywhere anyway, right?

Maybe this very simple example made just for illustration will helps to sort your thought:

rollout test ""
(
	local X = 0, Y = 0

	spinner spnX "X:" range:[0,100,0]
	spinner spnY "Y:" range:[0,100,0]
	edittext txtFormula "Enter formula:"
	spinner spnResult "Result:" range:[-1000,1000,0]
	button btnEval "Evaluate"

	on spnX changed val do (X = val)
	on spnY changed val do (Y = val)

	on btnEval pressed do
	(
		try(spnResult.value = execute txtFormula.text)
		catch(messageBox (getCurrentException()))
	)
)
CreateDialog test

-- now if you type in the editText field some valid expressions like:
"(X + Y) * 2", "(X * Y) / 3" -- end so on...
-- that will work fine
-- but staffs like:
"bla-bla" -- lol ... no way :)
"(pi * F) / N" -- ever this looks good but...
-- our UI support just X and Y for input
-- F and N are Undefined (in that example)

cheers!

I got errors

Rodman · 2012-11-01

.

?...

Anubis · 2012-11-01

Its possible to have some error as I not test the code, but I hope you get the idea.
Also really wonder, why you just not use the same variable name for your loop (ie TT):

formula = "cos((tt*2)*(180/pi))+tt"
for tt = 1 to 100 do execute(formula)

And if by reason you need to loop with other variable name, for example "For X = ... Do", then:

for x=1 to 100 do (tt=x; execute(formula))

The TT can be logal or global variable, is your choice.
So if you happy with Execute(), then that's all ;)

Look at execute()

Garp · 2012-11-01

It will tokenize and evaluate your string for you.

Thanks for your help.I have

Rodman · 2012-11-01

Thanks for your help.

I have just found a solution :


the_array = #()
for t = 0 to 100 do (append the_array t)

for t = 1 to the_array.count do
(
	formula = "(" + "cos((tt*2)*(180/pi))+tt" + ")"
	tokens = filterString formula "tt" splitEmptyTokens:true
	the_value = the_array[t] as string
	
	for i = 1 to tokens.count do
	(
		if tokens[i] == "" do
		(
			tokens[i] = the_value
		)	
	)

	formula = ""
	 
	format "tokens is %\n" tokens
	
	for i = 1 to tokens.count do
	(
		formula += tokens[i]
	)

	formula = execute formula
	format "formula is %\n" formula
)

But as you can see my formula must have tt as a variable and I would like to put t only, but function like tan will not work with my script. Do you have an idea how to do it ?