Re: how to convert variable name to a string?



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
.



Relevant Pages

  • Re: gfortran diagnostics and so on
    ... Well, in f0003, backslash is part of the standard Fortran character set. ... Because the backslash is part of the standard Fortran character set, the default behavior should be the printable character, **NOT** some kind of magic introductory character that transforms the interpretation of following character. ... The one I like best is to use one of the popular extensions to designate a particular literal string according to the C language. ...
    (comp.lang.fortran)
  • Re: Convert Char String "9.8E29" to Real
    ... Hey Tornado Jeff ... As Steve Kargl pointed out the Fortran way to convert a character ... how to convert a Character string like "9.8E29" to it's Real ...
    (comp.lang.fortran)
  • Several questions about character C binding
    ... I have many questions about character variables passed to a C ... I tested an interface passing a TYPE. ... obligatory the use of the C_LOC function from the FORTRAN side: ... the dimension of the string behind the scene as an additional argument ...
    (comp.lang.fortran)
  • Re: Returning a dynamic character array based on input length
    ... continues beyond my starting string length. ... Is there a way to check whether the line read from the input file is ... "String termination character" isn't the right concept here. ... Fortran ...
    (comp.lang.fortran)
  • Proposal: CHARACTER(expr, KIND = ...)
    ... However, internationalized character string support continues to increase in other languages, so it seems reasonable to expect it in Fortran eventually. ... Unlink simple numeric conversions, it would be good to have an error return indicating how many characters were replaced with the "place holder" character, which would probably be '*' by default, to correspond to erroneous output conversions. ...
    (comp.lang.fortran)