I want to parse and calculate formulas line by line. The formulas depend on the given variables x1 and x2. The number of formulas per line is not fixed, and there are dependencies between the results of the formulas, but the nesting level does not exceed two layers. I hope to calculate the final result y.
Here is some simulated data:
x1 = [1, 2, 3]
x2 = [4, 5, 6]
fml = ["y1=x1+x2; y2 = x1-x2; y=y1*y2", "y1=x1+x2; y2=x1-x2; y=y1*y2", "y1=x1+x2; y=y1+1"]
t = table(x1, x2, fml)
I tried to implement it by concatenating scripts, but there are issues.
s = "x1=" + string(t.x1) + "; " + "x2=" + string(t.x2) + "; " + t.fml + "; " + "y;"
runScript(s[0]) // OK
each(runScript, s) // returns [NULL, NULL, NULL]
Why does it work when executed separately, but not when iterating with each?
Or is it possible to achieve this through metaprogramming?