Re: how to convert variable name to a string?
- From: Ron Shepard <ron-shepard@xxxxxxxxxxxxxxxxxx>
- Date: Sun, 13 Jul 2008 08:46:43 -0500
In article
<4a9d63ca-e5b4-424a-b6ce-1ed8e132b25f@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Terence <tbwright@xxxxxxxxx> wrote:
dudes,
I could not find a way to convert a variable name to a string, or
convert a string to a variable name
e.g, character *100 x
real VAR1
x = "VAR1", how to use x to manipulate the VAR1?
best
Wei
I don't understand what you want to do.
If you want to convert the current value of a variable into a
chatacter string then you use the internal write command
WRITE(X,'format specifier') VAR1
where the format specifier is probably in the form Fw.d .
If it is the variable name itself you require as a string then
X='VAR1'
will form a 100 character length string with VAR1 as the first four
characters.
This is all correct, but what he wants to do is to use X to
read/write/modify the value of VAR1. In perl, for example, you
could use $X as a way to do this.
In the example code given, the same thing could be accomplished by
defining a pointer and doing a pointer assignment to VAR1. But this
does not address the more general problem, for example, if you want
to read in a character value from a user prompt to determine which
variable to change. In a language like perl or csh or sh, this is
straightforward, but it takes some work (as others have pointed out
already) to do it in fortran. Something like internal I/O using
namelist might be extended to do this, but for now there is no
direct support through the I/O library. So at present, the
programmer must use IF/THEN/ELSE or SELECT (for small numbers of
variables) or set up hash tables and searches (for larger numbers of
variables) to do this kind of thing in fortran. Or, if you can turn
the problem into an array indexing problem and work with the array
index (rather than a variable name), and then it is trivial in
fortran.
$.02 -Ron Shepard
.
- References:
- how to convert variable name to a string?
- From: cowbee
- Re: how to convert variable name to a string?
- From: Terence
- how to convert variable name to a string?
- Prev by Date: Re: rpc's comments on Fortran 2008, part 3
- Next by Date: Re: defined operator & assignment speed & memory usage problem
- Previous by thread: Re: how to convert variable name to a string?
- Next by thread: Re: how to convert variable name to a string?
- Index(es):
Relevant Pages
|
|