Convert string to float / array problem
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]) ) <code>
Comments
just a fun idea, maybe...
but try it out anyway :)
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 useformat()
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:
my recent MAXScripts RSS (archive here)
I got errors
.
Rodman
?...
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 ;)
my recent MAXScripts RSS (archive here)
I got errors
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.
Rodman
and...
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:
cheers!
my recent MAXScripts RSS (archive here)
well...
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).
my recent MAXScripts RSS (archive here)
Look at execute()
It will tokenize and evaluate your string for you.
Thanks for your help.I have
Thanks for your help.
I have just found a solution :
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 ?
Rodman