Assign variables via flexible strings

Hi Chris,

I am trying to assign a value to a variable with the assignment action where the variable name is defined by concatenating two strings (one is the core of variable name and the other if a suffix; e.g., var_name = ‘show_target’ aka the core, selected_XBI = ‘XBI07’ - the suffix).

assignment (
    variable = '${var_name}_${selected_XBI}'
    value = var_value
    )

But the problem is that I get an error during compilation:

00:36:29:  ERROR: Expression parser error: Syntax error at position 21 near ${selected_XBI}
Extended information:
	location: line 241, column 21
	object_type: action/assignment
	parent_scope: Training_task_1
	ref_id: idm35182573714016
	parser_context: mw_anonymous_create

And I can’t find a way to fix it. I tried various ways to define the string. If I just specify the full variable name as a string directly (variable = 'show_target_XBI07') then it works.

I hope there is a solution!

Best,
Vladyslav

Hi Vladyslav,

Sorry, but that isn’t going to work. An assignment action’s variable parameter must be a literal variable name, optionally followed by subscript expressions.

One alternative is to perform the assignment with Python code. Assuming var_value is the name of another MWorks variable containing the value to assign, this would work:

run_python_string("setvar(getvar('var_name') + '_' + getvar('selected_XBI'), getvar('var_value'))")

Cheers,
Chris

1 Like