Variable name to string [message #25780] |
Tue, 17 July 2001 13:50  |
Chad Bender
Messages: 21 Registered: July 2001
|
Junior Member |
|
|
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.
The program asks what variable should be output, the user gives the
variable name,
and somehow the program is able to get a string with contents equivalent
to the variable
name for use in opening the output file.
Seems like it should be an easy problem, but for the life of me I can't
figure it out.
In case it matters, I'm running IDL 5.4 under RedHat Linux 6.2.
Thanks
Chad Bender
Dept of Physics and Astronomy
State University of New York @ Stony Brook
|
|
|
Re: Variable name to string [message #25858 is a reply to message #25780] |
Wed, 18 July 2001 12:32  |
Chad Bender
Messages: 21 Registered: July 2001
|
Junior Member |
|
|
Thanks Craig. That is just what I was looking for. Running the program
at the main level is not a problem for me. Basically, I have a bunch of
data stored in array's and saved in an IDL save file. I just needed
something to convienently get them out of IDL and into individual text
files, while retaining the information contained in the array names.
Chad
On 18 Jul 2001, Craig Markwardt wrote:
>
> Chad Bender <cbender@mail.astro.sunysb.edu> writes:
>
>> Hello-
>>
>> Can anyone please point me towards the solution of the following
>> problem?
>
> 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
> ------------------------------------------------------------ --------------
>
>
|
|
|
Re: Variable name to string [message #25865 is a reply to message #25780] |
Wed, 18 July 2001 08:52  |
Pavel A. Romashkin
Messages: 531 Registered: November 2000
|
Senior Member |
|
|
Craig Markwardt wrote:
>
> But honestly, you should try to re-work your
> concept so you don't have to do that if you can.
Craig again hit the nail on the head. This whole idea of saving to files
which name is defined at runtime percludes further use of these files
from inside a program. They can only be used by the operator, because
there is no way to tell the code what file names are, and moreover, what
are variable names going to be once those files are restored. I ran into
this before, and found out that there always is a better way to organize
the data.
Cheers,
Pavel
|
|
|
Re: Variable name to string [message #25868 is a reply to message #25780] |
Wed, 18 July 2001 07:04  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
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
------------------------------------------------------------ --------------
|
|
|
Re: Variable name to string [message #25873 is a reply to message #25780] |
Wed, 18 July 2001 06:16  |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
Chad Bender wrote:
>
> 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.
> The program asks what variable should be output, the user gives the
> variable name,
> and somehow the program is able to get a string with contents equivalent
> to the variable
> name for use in opening the output file.
>
> Seems like it should be an easy problem, but for the life of me I can't
> figure it out.
>
> In case it matters, I'm running IDL 5.4 under RedHat Linux 6.2.
>
> Thanks
> Chad Bender
> Dept of Physics and Astronomy
> State University of New York @ Stony Brook
execute won't work by runtime
If the program should work by runtime you should think a bit about using
of structures
PRO example
str=''
READ ,'INPUT:',str
value=10
struct={str:value}
PRINT,TAG_NAMES(struct)
PRINT,struct.(0)
END
For structures useful routines are:
http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_source/idl_ html/dbase/download/text2tagname.tar.gz
struct={txt2tagname(str):value}
http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_source/idl_ html/dbase/download/text_position.tar.gz
x=tag_names(struct)
pos_x=tag_position(struct,x)
for i=0,n_elements(pos_x)-1 do begin
print,x[i]
print,struct.(pos_x[i])
endfor
For further routines and licensing please have a look at
http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_lib_intro.h tml
regards
Reimar
--
Reimar Bauer
Institut fuer Stratosphaerische Chemie (ICG-1)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
http://www.fz-juelich.de/icg/icg1/
============================================================ ======
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_lib_intro.h tml
http://www.fz-juelich.de/zb/text/publikation/juel3786.html
============================================================ ======
read something about linux / windows
http://www.suse.de/de/news/hotnews/MS.html
|
|
|