[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variable name to string




Chad Bender <cbender@mail.astro.sunysb.edu> writes:

> Hello-
> 
> Can anyone please point me towards the solution of the following
> problem?
> 
> Say I want to create a program that will write the contents of a named
> array to a
> file of the same name.  This could be accomplished by having the program
> prompt
> the user for both the variable name (getting the array by reference) and
> the file name
> (getting the string naming the output file), and the user just puts the
> same
> thing twice.  However, I would like to be able to do it so the user only
> has one prompt.

This appears to be basic string manipulation, which can be
accomplished using the "+" symbol.

;; Get variable name.
varname = ''
read, varname

;; Derive file name from variable name
filename = varname + '.dat'

Now, as the others have pointed out, the harder question is really,
how do you get at a variable for which you only have the name?
EXECUTE can work if the variable and your code are at the same level.
For example, this code

return_code = execute('DATA = '+varname)

will put the data into a variable named DATA.  Also, sneaky use of
ROUTINE_NAMES can do it.  But honestly, you should try to re-work your
concept so you don't have to do that if you can.

Craig


-- 
--------------------------------------------------------------------------
Craig B. Markwardt, Ph.D.         EMAIL:    craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
--------------------------------------------------------------------------