1

How can I automate the execution of a series of functions (with the names “test_1”, “test_2“, … “test_100”) using metaprogramming in DolphinDB?

For example:

def test_2(){
    return 1
}
eval(<test_2()>)//output: 1

I want to use a loop to generate the function names as strings and pass them to eval for execution. But when I passed the function name as a string to eval, the result is not what I wanted:

eval(<'test_2()'>)//output: test_2()
jinwandalaohu
  • 226
  • 1
  • 7

1 Answers1

1

You can use parseExpr() to convert the string “test_2()“ into metacode (<test_2()>), then execute it with eval(). To automate the execution of all 100 functions, simply write a loop that generates strings of the function names.

Alternatively, you can also use functionByName to dynamically execute a function. In your example:

 loop(x->funcByName("test_" + string(x))(), 1..100)

For more information about functionByName, see the DolphinDB official documentation.

YaN
  • 407
  • 1
  • 6