Search code examples
stringvariablesscopeidl-programming-language

How can I use scope_varname in IDL with a string construct representing the variable name?


I have an IDL function that takes in up to 4 data variables: data1, data2, data3 and data4. I want to be able to access the level=-1 scope of these variables in a loop using a string construct for the data variable name, so I can document the name of the original data that was passed to the function in an efficient manner.

Here's a simplified version of the function, showing only pertinent parts.

Function funcData, dat1, dat2, dat3, dat4,
  n=1 
  txt = "Data " 
  ;Check that data variable n was passed.                                      
  WHILE N_ELEMENTS(scope_varfetch("dat"+strtrim(n+1,1), level=0, /enter)) $
      NE 0 DO BEGIN
    dat = scope_varfetch("dat"+strtrim(n,1), level=0, /enter)    ; get data
    txt=txt + scope_varname("dat"+ strtrim(n,1), level=-1) +", " ; data names  
    n+=1                                                         ; update n 
  ENDWHILE
END

The problem is that scope_varfetch handles the concatenated string construct "dat"+strtrim(n,1) and returns the appropriate data set, but scope_varname does not, returning a blank.

Does anyone know why this is happening? Is there another way I can do this (short of brute force, case format)?

I have tried to search for an answer on-line, but have not been able to find anything about using string constructs in the IDL scope functions.


Solution

  • A Facebook contact provided this solution:

    result=execute('sv=scope_varname(dat'+ strtrim(n,1)+', level=-1)')   
    txt=txt + sv + ", "  
    

    Works perfectly.