From: dpb on
analyst41(a)hotmail.com wrote:
> On Jun 10, 8:10 pm, dpb <n...(a)non.net> wrote:
>> MuxaB wrote:
>>
>> ...
>>
>>> The question is: is there a way to convert that character string back
>>> into the variable name? That is, starting with CHAROUT='PRESSURE',
>>> figure out the variable name PRESSURE and use it to write out the
>>> data.
>> Short Fortran answer is "no"...
>>
>> --
>
>
> I think what we are talking about here is metadata. A database for
> example has column names and types along with the data and a select *
> would give you all the columns along with their names.

Yeah, one could implement something to parse data and handle it
dynamically but I take it the OP wanted something of the likes of the
interpreted languages such as, say, Matlab that have the facility to do
something like (to mix metaphors by using Matlab syntax to generate a
Fortran READ() :) )...

v = strvcat('P', 'X', 'Y') ; % pick some variable names
for n = 1:length(v)
eval(['read(11,*) ' v(n)]) ; % -->read(11,*) P for n==1, etc.
end

--
From: mecej4 on
MuxaB wrote:

> I have a FORTRAN question.
>
> The problems is:
>
> I want to write several variables, arrays and scalars, out to a file.
> I want to store the names of these variables in character strings to
> be used in the routine that writes the data out. For example, for the
> array called PRESSURE(istart:iend), I would store 'PRESSURE' in a
> character variable say CHAROUT of type character*10 (the PRESSURE
> array dimensions will be stored elsewhere).
>
> The question is: is there a way to convert that character string back
> into the variable name? That is, starting with CHAROUT='PRESSURE',
> figure out the variable name PRESSURE and use it to write out the
> data.

Variable names (and procedure names) need not even exist in the .EXE (or
a.out) file, although a compiler can put some symbols and line number
information into the executable for the benefit of a debugger or error
traceback runtime. The symbolic information is, for all practical purposes,
inaccessible in a Fortran program.

The question of converting a character string to something that does not
exist is, therefore, moot.

You can, however, do something along the following lines:

Subroutine WrVar(Var,Varname)
write(..,..)varname
write(..,..)var
return

and call the subroutine as

Call WrVar(PRESSURE,'Pressure')
call WrVar(TEMPR,'Temperature')
....

To handle variables of several types, you could make WrVar an overloaded
module procedure. You could write a generic procedure instead, or use
several versions for the different types.

There would be no protection from your doing something such as

call WrVar(PRESSURE,'Temperature')

-- mecej4
From: Ken Fairfield on
On Jun 10, 5:10 pm, dpb <n...(a)non.net> wrote:
> MuxaB wrote:
>
> ...
>
> > The question is: is there a way to convert that character string back
> > into the variable name? That is, starting with CHAROUT='PRESSURE',
> > figure out the variable name PRESSURE and use it to write out the
> > data.
>
> Short Fortran answer is "no"...

A longer answer is to consider weather Fortran's NAMELIST
input/output would accomplish what you want.

Similar to mecej4's answer, it requires whatever subroutine
does your input/output to "know" ahead of time which variables
you want to deal with, but NAMELIST is more succinct if it
suits your needs.

-Ken