how to convert variable name to a string?

how to convert variable name to a string?

Post by cowbe » Sun, 13 Jul 2008 07:08:02


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
 
 
 

how to convert variable name to a string?

Post by dpb » Sun, 13 Jul 2008 07:14:38


...

The reason you couldn't find a way is 'cause there isn't one.

That would be most closely approximated (in the one direction at least)
as an interpreter. I know I've seen references to such for Fortran but
don't have any direct references.

Perhaps more to the point, outlining what are you trying to accomplish
that you think you need this facility _might_ lead to alternatives.

 
 
 

how to convert variable name to a string?

Post by fj » Sun, 13 Jul 2008 18:38:43


I think that you are an a wrong way.

Languages like FORTRAN, C, C++ do not store the variables with an
associated name. They define them directly in the memory and access
them via an address. The compiler is in charge of translating the
"user" variable name VAR1 into the corresponding memory. This
translation occurs at compile time of course. But at running time, the
"user" name VAR1 does not exist anymore : just remains the address to
the corresponding memory !

In interpreted languages like PHP, python ... variables are often
stored into a hashtable (or something similar like a dictionary, a
data base ..) and keep their name at running time. This is why it
remains always possible to get them via another variable containing
their name (like your variable x). But the price is high :
performances are strongly degraded.

This is why compiled languages usually do not use this feature which
is reserve to languages where performance is not essential.
 
 
 

how to convert variable name to a string?

Post by fj » Sun, 13 Jul 2008 19:05:30


Just another comment : if the feature is essential for you, then you
need to create a derived type object which will play the role of
storing variables under a specific name. The variables you want to
access by name at runtime will have to be stored (registered) into
that object first.

This kind of derived type object already exists for FORTRAN codes.
Search hashtable in the present forum : you will possibly get
interesting answers.

I personnaly uses the SIGAL system which was dedicated to softwares
written in F77 and C. It is now being replaced by the ODESSA system
(Organization of Data Exchanges in Scientific Software Applications)
which is specifically designed for FORTRAN-90 codes but has interfaces
for F77, C and C++.

Unfortunately the last version of SIGAL as well as the ODESSA system
are not free tools !
 
 
 

how to convert variable name to a string?

Post by robi » Sun, 13 Jul 2008 23:24:15


The only way that I know of is to use NAMELIST.

You could write the output from NAMELIST
to a file, and then read back the second record of the file,
which contains the name of the variable.

But don't you already know what the name of the variable is?
 
 
 

how to convert variable name to a string?

Post by Gary Scot » Mon, 14 Jul 2008 00:36:45


That might be overly general. Often the names become names or labels in
an assembly or intermediate form as well prior to conversion to object
code. Names are often retained in the object code for debugging purposes.

--

Gary Scott
mailto:garylscott@sbcglobal dot net

Fortran Library: http://www.yqcomputer.com/

Support the Original G95 Project: http://www.yqcomputer.com/
-OR-
Support the GNU GFortran Project: http://www.yqcomputer.com/

If you want to do the impossible, don't hire an expert because he knows
it can't be done.

-- Henry Ford
 
 
 

how to convert variable name to a string?

Post by Terenc » Mon, 14 Jul 2008 20:34:25


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.





If you want to supply the contents
 
 
 

how to convert variable name to a string?

Post by Ron Shepar » Mon, 14 Jul 2008 22:46:43

In article
< XXXX@XXXXX.COM >,



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
 
 
 

how to convert variable name to a string?

Post by fernando.q » Tue, 15 Jul 2008 19:11:31

This is what I use:

with WRITE you can convert REAL into STRING
with READ you can convert STRING into REAL

WRITE(X,'format specifier') VAR1
READ(VAR1,'format specifier') X
 
 
 

how to convert variable name to a string?

Post by glen herrm » Sun, 20 Jul 2008 07:56:39


Yes, this is a property of many languages designed to be
interpreted, and few designed to be compiled. Still, Fortran
NAMELIST comes pretty close. If internal I/O using NAMELIST
is allowed, that might do what you need. It seems that internal
I/O requires sequential access Formatted I/O (Fortran 2003, 9.3),
and I don't see that NAMELIST doesn't qualify, but I am not
so sure that it does.


Yes, that would help.

-- glen