Fortran: How to make a long character made with an array

  • Context: Fortran 
  • Thread starter Thread starter hodei_cloud
  • Start date Start date
  • Tags Tags
    Array Fortran
Click For Summary

Discussion Overview

The discussion revolves around how to construct a long character string in Fortran using components from a character array. Participants explore different methods for concatenating characters and also consider extending the approach to handle matrices and arrays.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant shares an initial attempt to fill a string from a character array but acknowledges it is incorrect and seeks help.
  • Another participant provides a working example of how to concatenate characters from a one-dimensional array into a string using a loop.
  • A subsequent post repeats the working example and asks how to adapt the solution for a two-dimensional character array and a corresponding array for the output.
  • A different participant suggests a method for filling an array of strings from a two-dimensional character array, illustrating how to access substrings of the array elements.
  • One participant expresses relief at finding a solution to the character array issue, indicating the complexity of the topic.

Areas of Agreement / Disagreement

Participants generally agree on the methods for handling one-dimensional character arrays, but there is no consensus on the best approach for extending this to two-dimensional arrays, as the discussion includes multiple proposed methods.

Contextual Notes

Some participants' solutions depend on specific assumptions about array dimensions and character lengths, which may not be universally applicable. The discussion does not resolve the complexities involved in adapting the solutions for different array structures.

hodei_cloud
Messages
19
Reaction score
0
Lets see if I explain myself. I want to FILL a string with the components of a character array. I have no idea, but seeing this example:
Code:
character(len=*),parameter::fname="     Paul",lname="Scholes"
character(len=20)::fullname

fullname=fname//" "//lname
I try this one

Code:
character(len=1),dimension(5)::x
character(len=100)::string
x(1)='h'
x(2)='e'
x(3)='l'
x(4)='l'
x(5)='o'
do i=1,5
  a=a//x(i)
enddo
I know that its wrong, but I don't know how to make it...
Any help will be gratefull
 
Technology news on Phys.org
Code:
program atc
    character(len=1), dimension(5)::X
    character(len=20)::a
    
    a = ""
    x(1) = 'h'
    x(2) = 'e'
    x(3) = 'l'
    x(4) = 'l'
    x(5) = 'o'

    do i = 1, 5
        a(i:i) = x(i)
    end do
    
    write(*,*) 'a = ', a

end program atc
 
  • Like
Likes   Reactions: 1 person
gsal said:
Code:
program atc
    character(len=1), dimension(5)::X
    character(len=20)::a
    
    a = ""
    x(1) = 'h'
    x(2) = 'e'
    x(3) = 'l'
    x(4) = 'l'
    x(5) = 'o'

    do i = 1, 5
        a(i:i) = x(i)
    end do
    
    write(*,*) 'a = ', a

end program atc

Thank you!
And if I want to do the same thing but with "x" being a matrix and "a" a array?
I try some programms, but no luck
 
If you have
Code:
character(len=20), dimension(5) :: a
then
Code:
a(i)
is an element of the array, and
Code:
a(i)(j:k)
is a substring of the element.
Code:
character(len=1), dimension(5,5) :: x = reshape( (/(char(i+ichar('a')),i = 0,24)/), &
& shape(x) )          ! sets x(1,1) = 'a', a(2,1) = 'b',  ..., x(5,5) = 'y'
character(len=20), dimension(5) :: a = (/ (' ', i = 1,5) /)

do i = 1,5
   do j = 1,5
       a(i)(j:j) = x(j,i)
   end do
end do

do i = 1,5
   write(*,*) 'a(',i,') = ',a(i)
end do
end
 
Last edited:
  • Like
Likes   Reactions: 1 person
Thanks, the array of a character was driving me crazy... haha
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K