Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dynamic variable name

188 views
Skip to first unread message

Jinsong Zhao

unread,
May 9, 2011, 5:21:22 AM5/9/11
to
Hi there,

Is it possible to obtain variable name dynamically? In Fortran, I can
get file name or format dynamically through internal file, such as...

do i=1,n
write(filename, '(a,i0,a)') 'file_', i, '.txt'
end do

Is there a similar way for variable name?

Any suggestions will be really appreciated. Thanks in advance!

Regards,
Jinsong

rudra

unread,
May 9, 2011, 5:29:13 AM5/9/11
to
On May 9, 11:21 am, Jinsong Zhao <jsz...@yeah.net> wrote:
> Hi there,
what it seems you want to concatenate the file name, whic is
absolutely possible as:

do i=1,ntype
filein='fileprefix'//ACHAR(i)//'.file_type' !in any oreder
end do

Arjen Markus

unread,
May 9, 2011, 5:50:59 AM5/9/11
to

No, Fortran is not a dynamic language in that respect.

Could you clarify how you would use it? Maybe we can help you find a
different solution.

Regards,

Arjen

mecej4

unread,
May 9, 2011, 6:53:22 AM5/9/11
to
You are confusing yourself.

In your code snippet, "filename" is a variable name. You would have
declared it to be a character type variable, and as used in the internal
WRITE statement it should be a character type variable. What the
internal write statement changed was the _value_ of the variable, not
its name.

Had you not used any internal write statement, "filename" could still
have had its value changed in several ways, none of which need to be
associated with a file or with I/O. For example, the variable could have
stood for the name of your town in a census data file.

Similarly, variables of other types can have their values changed in
several ways. However, in a compiled language such as Fortran, not only
can you not change the variable name at run time; you cannot even ask
what the variable name. Unless the run image contains debug symbols,
there are usually no variable names in the image. If you did not enable
traceback or trap handling, even subprogram names may be absent.

-- mecej4

glen herrmannsfeldt

unread,
May 9, 2011, 7:45:27 AM5/9/11
to
Jinsong Zhao <jsz...@yeah.net> wrote:

> Is it possible to obtain variable name dynamically? In Fortran, I can
> get file name or format dynamically through internal file, such as...

(snip)


> Is there a similar way for variable name?

There is NAMELIST, normally used when that is the requirement.

I believe that NAMELIST can be used with internal files,
though I haven't tried it recently. You do have to specify
the list of names (see where they got the name from) that you
want to use, but after you do that, the system does the rest.

-- glen

Jinsong Zhao

unread,
May 9, 2011, 10:18:00 AM5/9/11
to
On 2011-5-9 18:53, mecej4 wrote:
> On 5/9/2011 4:21 AM, Jinsong Zhao wrote:
>> Hi there,
>>
>> Is it possible to obtain variable name dynamically? In Fortran, I can
>> get file name or format dynamically through internal file, such as...
>>
>> do i=1,n
>> write(filename, '(a,i0,a)') 'file_', i, '.txt'
>> end do
>>
>> Is there a similar way for variable name?
>>
>> Any suggestions will be really appreciated. Thanks in advance!
>>
>> Regards,
>> Jinsong
> You are confusing yourself.
>

you are right, i am confusing myself, and at the same time confusing the
one here...

> In your code snippet, "filename" is a variable name. You would have
> declared it to be a character type variable, and as used in the internal
> WRITE statement it should be a character type variable. What the
> internal write statement changed was the _value_ of the variable, not
> its name.

the code snippet is just a example, and demo that Fortran can do such
kind of things, as you stated that the code change the value of the
variable `filename'.

>
> Had you not used any internal write statement, "filename" could still
> have had its value changed in several ways, none of which need to be
> associated with a file or with I/O. For example, the variable could have
> stood for the name of your town in a census data file.
>

as for the `filename' in the code snippet, i can understand what you
said here.

> Similarly, variables of other types can have their values changed in
> several ways. However, in a compiled language such as Fortran, not only
> can you not change the variable name at run time; you cannot even ask
> what the variable name. Unless the run image contains debug symbols,
> there are usually no variable names in the image. If you did not enable
> traceback or trap handling, even subprogram names may be absent.
>

What I hope to know is whether Fortran could produce a series variable
name, such as v_1, v_2, v_3, and then I could assign specific value to them.

As Arjen said in the previous post, it is impossible in Fortran, :-(

Thanks a lot.

Regards,
Jinsong

> -- mecej4

Arjen Markus

unread,
May 9, 2011, 10:25:00 AM5/9/11
to
> > -- mecej4- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -

Note that while you can not do this with the variables themselves, a
workaround
is to use a dictionary:

type dictionary_value
character(len=20) :: name
integer :: value ! Or something like that
end type

type dictionary
type(dictionary_value), dimension(:), allocatable :: key_value
end type

call set(dictionary, "var1", 1)
call set(dictionary, "var2", 100)
write(*,*) get(dictionary, "var2")

Or, using F2003 (and some adjustments to the type to get type-bound
procedures):
call dictionary%set( "var1", 1 )
call dictionary%set( "var2", 100 )
write(*,*) dictionary%get("var2")

It all depends on what you want to achieve.

Regards,

Arjen

Jinsong Zhao

unread,
May 9, 2011, 10:29:33 AM5/9/11
to

Thanks for the hints. I am confusing almost everyone. what i need is not
related with file names.

I need something like:

In a file, there is a line: "123 345 567". When read, I hope it could
automatic assign to three variables, e.g., v_1, v_2, v_3, which not
declared beforehand. it seems to be impossible...

Regards,
Jinsong

Jinsong Zhao

unread,
May 9, 2011, 11:14:54 AM5/9/11
to
On 2011-5-9 22:25, Arjen Markus wrote:
[snip]

>
> Note that while you can not do this with the variables themselves, a
> workaround
> is to use a dictionary:
>
> type dictionary_value
> character(len=20) :: name
> integer :: value ! Or something like that
> end type
>
> type dictionary
> type(dictionary_value), dimension(:), allocatable :: key_value
> end type
>
> call set(dictionary, "var1", 1)
> call set(dictionary, "var2", 100)
> write(*,*) get(dictionary, "var2")
>
> Or, using F2003 (and some adjustments to the type to get type-bound
> procedures):
> call dictionary%set( "var1", 1 )
> call dictionary%set( "var2", 100 )
> write(*,*) dictionary%get("var2")
>
> It all depends on what you want to achieve.
>
> Regards,
>
> Arjen


Thank you very much.

About dictionary, I don't find any information in the Fortran 2003
Handbook. Would you please point me more information.

Thanks!

Regards,
Jinsong

e p chandler

unread,
May 9, 2011, 11:27:09 AM5/9/11
to

dictionary is not a construct built in to Fortran, it is the name of a
variable. What Arjen was simulating in Fortran is what some other
languages call an associative array. The "subscript" into that array
is a string. The contents of that variable have a value. So in some
other language you might say

dictionary["var_1"] = 123
dictionary["var_2"] = 234
....

Using some more modern language features it is possible to set up an
entire type (or class) of variables with the same abilities:

number_of_customers["smith"] = 42
number_of_customers["jones"] = 59

This is almost like saying #simth# = 42 or #jones# = 59.

If you are trying to pre-process some data that is then used as input
to a Fortran program, consider using a scripting language that does
have these facilities to prepare the input data. Examples are Perl,
AWK, SNOBOL and others.

--- e

Jugoslav Dujic

unread,
May 9, 2011, 11:34:47 AM5/9/11
to

Yes, it is impossible in the general case. That is true for all compiled
languages: once the compiler compiles with your code, the resulting
executable file "forgets" all the identifiers in your code, that were
used to make it.

There are certain, less general, solutions, which might suit your
need... if you only tell us what it is.

As Arjen hinted, you can make your own dictionary (list of pairs of
supported variable names and their values), and write code that
interprets its contents. Its limit is that the set of supported
variables (the ones in your code) must be set in advance.

As Glen said, there is NAMELIST, which is supported for file I/O,
but that has the same limit that only a set of variables already
coded in your program can be entered.

There is a more complex package, called "Fortran parser", which
presents a relatively simple interpreter:

http://stu.ods.org/fortran/

It lets user (in run-time) define their own variable names AND
formulas (operations) over these variables. However, it is limited
only to relatively simple algebra (such as Fortran functions).
Disclaimer: I've used only the "simple" version. "Advanced"
version looks much more powerful, but I didn't try it.

--
Jugoslav
http://www.xeffort.com

glen herrmannsfeldt

unread,
May 9, 2011, 1:01:20 PM5/9/11
to
Jinsong Zhao <jsz...@yeah.net> wrote:
> On 2011-5-9 19:45, glen herrmannsfeldt wrote:
>> Jinsong Zhao<jsz...@yeah.net> wrote:

>>> Is it possible to obtain variable name dynamically? In Fortran, I can
>>> get file name or format dynamically through internal file, such as...

(snip, then I wrote)


>> There is NAMELIST, normally used when that is the requirement.

>> I believe that NAMELIST can be used with internal files,
>> though I haven't tried it recently. You do have to specify
>> the list of names (see where they got the name from) that you
>> want to use, but after you do that, the system does the rest.

> Thanks for the hints. I am confusing almost everyone.

> what i need is not related with file names.

NAMELIST is not related to file names. Traditionally it only
worked reading and writing actual files. Your first post didn't
say it was from a file, and if you want to do it without an
actual file, you need to do it as in internal file.

> I need something like:

> In a file, there is a line: "123 345 567". When read, I hope it could
> automatic assign to three variables, e.g., v_1, v_2, v_3, which not
> declared beforehand. it seems to be impossible...

Well, that doesn't sound so far from an array. For NAMELIST
the actual variable needs to be declared, but you specify which
one in the data being read.

The Fortran 2003 example looks like:


INTEGER I; REAL X (8); CHARACTER (11) P; COMPLEX Z;
LOGICAL G
NAMELIST / TODAY / G, I, P, Z, X
READ (*, NML = TODAY)

The input data records are:

&TODAY I = 12345, X(1) = 12345, X(3:4) = 2*1.5, I=6, ! This is a comment.
P = ''ISN.T_BOB'S'', Z = (123,0)/


Note that actual variable names are specified with the input data.
You can read in as many or as few variables as you like (even none).

Or, for array elements, again as many or few as you like.

Normally, though, if you want to find variables by number, you
use arrays.

-- glen

Ron Shepard

unread,
May 9, 2011, 1:21:16 PM5/9/11
to
In article <iq8t2s$k2c$1...@dont-email.me>,
Jinsong Zhao <jsz...@yeah.net> wrote:

> What I hope to know is whether Fortran could produce a series variable
> name, such as v_1, v_2, v_3, and then I could assign specific value to them.

Have you read about NAMELIST I/O? That may be what you want to do.



> As Arjen said in the previous post, it is impossible in Fortran, :-(

Maybe, maybe not. It isn't exactly clear what you want to do, and
your example did not explain anything.

$.02 -Ron Shepard

Ken Fairfield

unread,
May 9, 2011, 1:27:13 PM5/9/11
to
On May 9, 7:18 am, Jinsong Zhao <jsz...@yeah.net> wrote:
[...]

> What I hope to know is whether Fortran could produce a series variable
> name, such as v_1, v_2, v_3, and then I could assign specific value to them.

I think folks have gotten side-tracked by
the notion of having a dynamic variable
name. But for the OP, how would you
know to reference that dynamic name
in the rest of the code to do anything
with it? It's sort of an oxymoron...

For the problem at hand, the common,
direct solution is to read into an array.
For the example you give, if you had
asked,

"could Fortran read into a series of array
locations, v(1), v(2), v(3), and then I could
assign specific value to them,"

the answer would be a resounding "yes".

There are various ways of doing this in
a READ statement. A Fortran text would
give some examples. But in the simplest
case, lets say an input line contains three
floating point values, and you've declared
"v" to be a REAL array of length three:

real :: v(3)
integer :: input_unit
...
read(input_unit,*) v

will read those three values from "input_unit"
and assign them to v(1), v(2) and v(3).

Does that help?

-Ken
and


glen herrmannsfeldt

unread,
May 9, 2011, 1:44:03 PM5/9/11
to
Ken Fairfield <ken.fa...@gmail.com> wrote:

(snip)

> For the problem at hand, the common,
> direct solution is to read into an array.
> For the example you give, if you had
> asked,

> "could Fortran read into a series of array
> locations, v(1), v(2), v(3), and then I could
> assign specific value to them,"

> the answer would be a resounding "yes".

> There are various ways of doing this in
> a READ statement. A Fortran text would
> give some examples. But in the simplest
> case, lets say an input line contains three
> floating point values, and you've declared
> "v" to be a REAL array of length three:

I have even done ones reading in 123 and then selecting
array elements based on n/100, mod(n/10,10), and mod(n,10)

-- glen

Gib Bogle

unread,
May 9, 2011, 5:13:12 PM5/9/11
to
On 5/10/2011 2:29 AM, Jinsong Zhao wrote:

> Thanks for the hints. I am confusing almost everyone. what i need is not
> related with file names.
>
> I need something like:
>
> In a file, there is a line: "123 345 567". When read, I hope it could
> automatic assign to three variables, e.g., v_1, v_2, v_3, which not
> declared beforehand. it seems to be impossible...

What advantage does using variable names v_1, v_2, v_3 have over using
an array v(:), such that you would read the values from your file into
the array, and access the values as v(1), v(2), v(3)? You have everyone
baffled as to your purpose.

James Van Buskirk

unread,
May 9, 2011, 6:37:06 PM5/9/11
to
"Jugoslav Dujic" <jdu...@yahoo.com> wrote in message
news:4DC80997...@yahoo.com...

> Yes, it is impossible in the general case. That is true for all compiled
> languages: once the compiler compiles with your code, the resulting
> executable file "forgets" all the identifiers in your code, that were
> used to make it.

But can't you have the program generate code and then invoke the
compiler to make a *.DLL file and then link to it with LoadLibrary?
Of course the program can't access the variables directly under the
chosen names, but the names will be available in the *.DLL file
for the Fortran program to access the data via the dynamic names
in the *.DLL file.

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


robin

unread,
May 9, 2011, 7:17:24 PM5/9/11
to
"Jugoslav Dujic" <jdu...@yahoo.com> wrote in message news:4DC80997...@yahoo.com...

The latter part of your statement is not correct.
Both Fortran and PL/I retain the names of certain variables.
In the case of Fortran, when NAMELIST is used, the names listed
are retained in the executable.
PL/I goes further. With data-directed I/O, the names of variables
given in the list of a GET DATA and PUT DATA statements
are retained in the executable. When no data list is used (as with GET DATA; and PUT DATA;),
the executable contains the names of ALL the variables within scope,
as in a dictionary.
On input (as with GET DATA;), this dictionary list is searched whenever a name is used
in the input data stream. [The input data stream may look like TOM = 123, X = 4;
in which case the dictionary is searched for the names TOM and X.
If the input data stream contains a name that does not exist in the dictionary list,
the error condition is raised, and the program can obtain the offending name.]


Nasser M. Abbasi

unread,
May 9, 2011, 11:31:36 PM5/9/11
to

good question.

fyi, Matlab has a function, which does exactly what the
OP wants, it is called:

deal()

This is how it works (it is meant to work on cell arrays)

----------------------
A={'hello','world',pi} %make a cell array
[v_1,v_2,v_3]=deal(A{:}) %assign its content to variables


v_1 =
hello

v_2 =
world

v_3 =
3.1416

-----------------------

I only had to use one time.

--Nasser

Richard Maine

unread,
May 10, 2011, 1:35:09 AM5/10/11
to
Ken Fairfield <ken.fa...@gmail.com> wrote:

> I think folks have gotten side-tracked by
> the notion of having a dynamic variable

> name. ...


> For the problem at hand, the common,
> direct solution is to read into an array.
> For the example you give, if you had
> asked,
>
> "could Fortran read into a series of array
> locations, v(1), v(2), v(3), and then I could
> assign specific value to them,"
>
> the answer would be a resounding "yes".

Indeed, at a suitable level of abstraction (and not a horribly high one
even), an array can be thought of as a collection of variables that are
"named" with integer names. In some sense, a "normal" array could be
considered as a spacial case of an associative array with the "names"
restricted to being integers.

--
Richard Maine
email: last name at domain . net
domain: summer-triangle

0 new messages