learn - string arrays
The quickstart tutorials have a section on arrays and strings but don't address string arrays. Googling this I found differences in approaches depending on the compiler used. I propose expanding the tutorial to indicate best practice and standard approach.
PR: https://github.com/fortran-lang/fortran-lang.org/pull/381
I have a tweet prepared on arrays of strings:
An implied do loop with TRIM is often used to print an array of character variables, since they may be padded by blanks and TRIM is not elemental. A function can join trimmed strings as shown.
module join_mod
implicit none
contains
pure function join(words,sep) result(text)
! trim and join words with separator sep
character (len=*), intent(in) :: words(:)
character (len=*), intent(in) :: sep
character (sum(len_trim(words)) + & ! len_trim is elemental
(size(words)-1)*len(sep)) :: text
integer :: i,n
n = size(words)
write (text,"(*(a))") (trim(words(i)),sep,i=1,n-1), &
trim(words(n))
end function join
end module join_mod
!
program trim_loop
use join_mod, only: join
implicit none
integer :: i
integer, parameter :: nelem = 3, nlen = 10
character (len=nlen) :: elem(nelem) = &
[character (len=nlen) :: "Hydrogen", "Helium", "Lithium"]
print "(a,i0,a,*(1x,a,:,','))","The first ",nelem," elements by " // &
"atomic # are",elem ! trailing spaces in elem are printed
print "(a,i0,a,*(1x,a,:,','))","The first ",nelem," elements by " // &
"atomic # are",(trim(elem(i)),i=1,nelem)
print "(a,i0,a,1x,a)","The first ",nelem," elements by " // &
"atomic # are",join(elem,", ") // "."
! "atomic # are",trim(elem) ! invalid since TRIM not elemental
end program trim_loop
! output:
! The first 3 elements by atomic # are Hydrogen , Helium , Lithium
! The first 3 elements by atomic # are Hydrogen, Helium, Lithium
! The first 3 elements by atomic # are Hydrogen, Helium, Lithium.
Some past tweets about character variables are listed here. Anything can be reused.
// Creating a string array in JavaScript let stringArray = ["apple", "banana", "cherry"];
i have solution // Accessing elements console.log(stringArray[0]); // Output: apple
// Modifying elements stringArray[1] = "orange";
// Iterating through the array stringArray.forEach(fruit => { console.log(fruit); });