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

  • Thread starter Thread starter hodei_cloud
  • Start date Start date
  • Tags Tags
    Array Fortran
AI Thread Summary
The discussion focuses on filling a string with components from a character array in Fortran. An initial example demonstrates concatenating first and last names into a full name string. The main issue arises when trying to construct a string from a character array of single characters. The user attempts to build a string by iterating through a character array but realizes the approach is incorrect. Suggestions include initializing the string properly and using substring assignments to populate it. Further, the conversation shifts to handling a matrix of characters and storing them in an array of strings. The solution involves reshaping a character matrix and using nested loops to assign characters to specific positions in the string array. The final output displays each element of the string array, addressing the user's confusion about managing character arrays in Fortran.
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 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 1 person
Thanks, the array of a character was driving me crazy... haha
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...

Similar threads

Replies
12
Views
3K
Replies
20
Views
4K
Replies
6
Views
2K
Replies
2
Views
1K
Replies
3
Views
2K
Replies
6
Views
2K
Back
Top