- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How can I mix two variables with different types in fortran. I intend to have a fixed name (character) such as Name1 = "File". Another variable, Number, will be defined as a variable (character) which may change by model input or within the code. For example, Number may begivenas"1", "2", or "1001","3001".Then I need to combine Name1 and Number so that the newvariable (Name2)is named as "File1", or "File2", or "File1001", or "Filr3001", which are obtained by adding Name1 ("File") and Number ("1", :2", "1001", or "3001").
Do you know how to do it?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Do you want to create a character variable whose value consists of a fixed prefix and a variable suffix? If so, then you use the "internal write" feature to do that, for example:
character(10) name
write (name,'(A,I0)') 'File', 1001
When run, this assigns the value 'File1001' to variable "name".
If you actually want to create a Fortran variable this way, so that the name of the variable changes at run-time, no, you can't do that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Consider using the '//' operator
character(10) name
character(4)Anumber
integer number
number = 1234! pick up integer number
write(Anumber,100) number
100 format(I0)
name ='File'//Anumber
...
Anumber = ':2'
name ='File'//Anumber
...
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, Jim. Your example work well if the input (number) is an integer. What if my input is characters or strings (such as, ":Case #1", or ":Case #A100"), which is Anumber in your example, which will be input from screen by the users? In other words, I hope to have the following:
name=File:Case #1 ; or
name=File:Case #100
Any more ideas?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Would you mind letting me know where to find the formatting flags that you put in the quotes in your WRITE statement? (e.g. the '(A,I0)' part of "write (name,'(A,I0)') 'File', 1001")
Thanks,
Jason C.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page