Here’s a small program that prints a 3x4 complex matrix with a preamble, surrounds each row with square brackets, and uses a repeat count to print the elements of each row. It uses format reversion to print each row on a separate line without repeating the preamble.
implicit none
complex, dimension(3,4) :: a = reshape(cmplx([1,2,3,4,5,6,7,8,9,10,11,12]),[3,4],order=[2,1])
integer :: i
a = a + (0.0,1.0)
print *
print '("Here''s the matrix with format 1:",/,("[",1x,4(S,G0,SP,G0,"i",1x),"]"))', (a(i,:), i=1,3)
print *
print '("Here''s the matrix with format 2:",/,("[",1x,4(s,g0,sp,g0,"i",",",1x),"]"))', (a(i,:), i=1,3)
print *
end
Result with gfortran 9.4.0
Here's the matrix with format 1:
[ 1.00000000+1.00000000i 2.00000000+1.00000000i 3.00000000+1.00000000i 4.00000000+1.00000000i ]
[ 5.00000000+1.00000000i 6.00000000+1.00000000i 7.00000000+1.00000000i 8.00000000+1.00000000i ]
[ 9.00000000+1.00000000i 10.0000000+1.00000000i 11.0000000+1.00000000i 12.0000000+1.00000000i ]
Here's the matrix with format 2:
[ 1.00000000+1.00000000i, 2.00000000+1.00000000i, 3.00000000+1.00000000i, 4.00000000+1.00000000i, ]
[ 5.00000000+1.00000000i, 6.00000000+1.00000000i, 7.00000000+1.00000000i, 8.00000000+1.00000000i, ]
[ 9.00000000+1.00000000i, 10.0000000+1.00000000i, 11.0000000+1.00000000i, 12.0000000+1.00000000i, ]
In the first instance the elements are separated by spaces. In the second instance I added comma separators, but I’m wondering how to omit the comma after the last element of each row. A :
within the repeated segment only terminates the final reversion, and omits the final comma and ending bracket on the last row only. Is there a way to do this without manually splitting out the last repetition?