Search code examples
trace32lauterbach

How to get a variable's value using it's string name


I have a global MACRO defined in .cmm script and I want to get it's value by using the string name.

I tried to reference it with double "&" symbol, but after this line I get an "operand expected" error:

    &error_name = STRING.LoWeR("&error_name")+"_low"
    &modulus=&&error_name
    VAR.IF (FS_ErrorIdStatus[&modulus] != 0)
    (
      &error_name = "ERR_MON_"+STRing.UPpeR("&error_name")+"_VOLTAGE"
      GOSUB TC_DEBUG_MESSAGE " --             FAILED: " + &error_name + " was reported"
      RETURN FALSE()
    )
 

Although in the debug window it's value is shown. (https://i.sstatic.net/6wspz.png)


Solution

  • You need two & on the left side of the assignment. Doing so will cause the macro expansion on the right side to happen twice before the assignment. Example:

    &some_macro=0x42
    
    ;construct macro name
    &macroname="some"+"_macro"
    
    ;get value from macro
    &&val=&&macroname
    
    PRINT &val
    

    After the first macro replacement run, &macroname is replaced with some_macro, in the second run, &some_macro is replaced with its value 0x42 which is then assigned to value &val.