way Fortran handles empty spaces - fortran

I would like to have some clarifications about the way Fortran handles "empty" characters in strings.
Let us assume we have this situation:
program main
implicit none
test('AB')
end program
where
function test(name)
implicit none
character(10) :: name
character(3) :: cutname
write(*,*) '-'//name//'-' ! Gives output "-AB -"
! Space has then been added at the end
cutname(1:3) = name(1:3)
write(*,*) '1-'//cutname//'-' ! Gives output "-AB -"
! It seems there is a space then at the end
! of cutname
write(*,*) (cutname(1:2) == 'AB') ! Gives output T (true)
write(*,*) (cutname(3:3) == ' ') ! Gives output F (false)
write(*,*) (cutname == 'AB ') ! Gives output F (false)
end function
I am pretty curious about what is happening in this case.
Thanks in advance.

Standard strings in Fortran are fixed length. If you don't use the entire string, they are padded on the end with blanks/spaces.
I altered your example program to pass compiler checks of gfortran and ifort. Your function had no return, so better as a subroutine. The compilers noticed the inconsistency between the lengths of the actual and dummy argument -- because I put the procedure into a module and useed it so that the compiler could check argument consistency. They complain about passing a length 2 string to a length 10 string. How are the remaining characters supposed to be defined?
module test_mod
contains
subroutine test(name)
implicit none
character(10) :: name
character(3) :: cutname
write(*,*) '-'//name//'-' ! Gives output "-AB -"
! Space has then been added at the end
cutname(1:3) = name(1:3)
write(*,*) '1-'//cutname//'-' ! Gives output "-AB -"
! It seems there is a space then at the end
! of cutname
write(*,*) (cutname(1:2) == 'AB') ! Gives output T (true)
write(*,*) (cutname(3:3) == ' ') ! Gives output F (false)
write(*,*) (cutname == 'AB ') ! Gives output F (false)
end subroutine test
end module test_mod
program main
use test_mod
implicit none
call test('AB ')
end program
When I run this version, the outputs are T, T and T, which is what I expect.
EDIT: I suggest using full warning and error checking options of your compiler. That is how I quickly found the issues with the example. With gfortran: -O2 -fimplicit-none -Wall -Wline-truncation -Wcharacter-truncation -Wsurprising -Waliasing -Wimplicit-interface -Wunused-parameter -fwhole-file -fcheck=all -std=f2008 -pedantic -fbacktrace.
A string assignment statement doesn't require the two sides to have the same lengths. If the RHS is shorter than the string variable on the LHS, it will get padded on the end with blanks. Here, the arguments should be consistent, including in length.

Related

Combining strings and integers to make a variable format: Missing initial left parenthesis in format (Fortran)

I want to make a variable format for my "write" statement. To do so, I wrote a small program (nvari is the variable):
program VariableFormat
implicit none
integer :: x = 1,y = 2, z = 3, i, nvari
double precision :: pi = 3.14
integer, allocatable :: var(:)
integer :: A(3) = (/1,2,3/)
character(100) :: fmt,fmt2,str1,str2,str3
print*, size(A)
allocate(var(size(A)))
do i = 1, size(A)
var(i) = A(i)
end do
nvari = 2
!
! first part
!
fmt = '(a,f4.2)'
write(*,fmt) "The value of pi is ", pi
!
! second part
!
write (str1, "(1A2,1I1,1A1,1I2,1A1)") "'(", 3, "I", 15, ","
print*, str1
write(str2,'(I10)') nvari
print*, str2
write (str3, "(1A1,1I2,1A2)") "I", 15, ")'"
print*, str3
fmt2 = trim(adjustl(str1))//trim(adjustl(str2))//trim(adjustl(str3))
print*, fmt2
write(*,fmt2) x,y,z,(var(i),i=1,nvari)
end program VariableFormat
First question:
It can be seen that fmt2 is similar to fmt in the sense that in both we have '(X,Y)'.
However, while the first part of the code is executed correctly, for the second part, I have the error:
At line 32 of file VariableFormat.F90 (unit = 6, file = 'stdout')
Fortran runtime error: Missing initial left parenthesis in format
'(3I15,2I15)'
I don't understand which parenthesis is missing as fmt2 is similar to fmt.
To execute my program I use the gfortran compiler: gfortran -o binary_VariableFormat VariableFormat.F90 and then:./binary_VariableFormat
Second question:
Is there a way to write fmt2 in a more compact way?
This is perhaps more a misunderstanding about character variables and literal constants than about formats.
In
fmt = '(a,f4.2)'
we have the character variable's name (fmt) on the left-hand side of the assignment and the value to assign on the right. '(a,f4.2)' is a literal character constant and its value is given to fmt.
The two, matching, ''s act as delimiters for the value of the constant: they are not themselves part of the value.1 The value of the literal constant is (a,f4.2). Go on, print the value of fmt to check. (One actually has to do something quite different to get the value of a character variable with delimiters.)
A character value in a format must be something like (...): the first non-blank value must be a (. Because you've set the value to be like '(...)' the compiler is (quite rightly) complaining.
Where we have something like
write(str2,'(I10)') nvari
we are using this literal constant which has value (I10) not '(I10)'. If we instead had
write(str2,"'(I10)'") nvari
we'd see exactly the same complaint (or perhaps a complaint when compiling instead of running).
Finally, note that when using a FORMAT statement (many here really don't like such things), we'd not have these delimiters at all:
format (I10) ! Correct
format '(I10)' ! Badly wrong
1 Similarly, if we have literal constants used like
complex :: a = (1., 2.)
integer :: i = INT(B'001')
the delimiters for the complex and BOZ literal constants are not part of the value. Equally in an array constructor like [0,1,2,3] the square brackets are a delimiter part of the syntax rather than the value.

line continuation character is not working

I have been using fortran 77 for several months and line continuation character doesn't work sometimes.
if((b1(j).eq.0d0).and.(b3(j).ne.0d0))then
write(22,*) a1(j),a2(j),a3(j),a4(j),a5(j),a6(j),a7(j),a8(j),
& a9(j),a10(j),a11(j),a12(j),a13(j),a14(j),a15(j),
& b3(j),0.2
else if((b3(j).eq.0d0).and.(b1(j).ne.0d0))then
write(22,*) a1(j),a2(j),a3(j),a4(j),a5(j),a6(j),a7(j),
& a8(j),a9(j),a10(j),a11(j),a12(j),a13(j),
& a14(j),a15(j),b1(j),b2(j)
end if
This is the code and when I compile it, the compiler says that '&' is a invalid character. What I am really curious is that sometimes it works and sometimes it does not. Probably I have a basic misunderstanding in fortran syntax.
final.f:59:72: Error: Expected expression in WRITE statement at (1)
final.f:60:9:
& b3(j),0.2
1
Error: Invalid character in name at (1)
final.f:63:72: Error: Expected expression in WRITE statement at (1)
final.f:64:9:
& a14(j),a15(j),b1(j),b2(j)
1
Error: Invalid character in name at (1)
Anybody can help me?
parameter(m=813,n=616)
real*8 a5(m),a6(m),a7(m),a8(m),a9(m),a10(m),a11(m),a12(m),a13(m)
integer*8 a1(m),a2(m),a3(m),a4(m),p1(n)
real*8 p2(n),p3(n),p4(n),p5(n),p6(n),a14(m),a15(m)
real*8 b1(m),b2(m),b3(m)
character skip80*80
open(11,file='dist.dat')
open(12,file='mksa.dat')
open(13,file='mksb.dat')
open(14,file='mksc.dat')
open(15,file='mksi.dat')
open(22,file='ksaf.dat')
open(23,file='ksbf.dat')
open(24,file='kscf.dat')
open(25,file='ksif.dat')
read(12,*)skip80
do i=1,m
read(12,*) a1(i),a2(i),a3(i),a4(i),a5(i),a6(i),a7(i),
& a8(i),a9(i),a10(i),a11(i),a12(i),a13(i),a14(i),a15(i)
end do
do i=1,n
read(11,*) p1(i),p2(i),p3(i),p4(i),p5(i),p6(i)
end do
do j=1,m
do i=1,n
if(a1(j).eq.p1(i))then
b1(j)=p4(i)
b2(j)=p5(i)
b3(j)=p6(i)
end if
end do
if((b1(j).eq.0d0).and.(b3(j).ne.0d0))then
write(22,*) a1(j),a2(j),a3(j),a4(j),a5(j),a6(j),a7(j),a8(j)
& ,a9(j),a10(j),a11(j),a12(j),a13(j),a14(j),a15(j)
& ,b3(j),0.2
else if((b3(j).eq.0d0).and.(b1(j).ne.0d0))then
write(22,*) a1(j),a2(j),a3(j),a4(j),a5(j),a6(j),a7(j)
& ,a8(j),a9(j),a10(j),a11(j),a12(j),a13(j)
& ,a14(j),a15(j),b1(j),b2(j)
end if
end do
end

Error counting substrings from file in Fortran 95

I'm trying to write a code in Fortran 95 that read strings from a file and then print a frequency table of letters from the file. The file has a text like the example below, with no more than 128 characters per line:
Ablblb lbla sdrtwfwefw
Waerfaw efeafawef awef
Pefwae fwefawefw efawe
Fcicnj ioejo, o njcdid
Pweko jai, wadwed awdd
So I did one program that is working fine for the first letter (A or a), but the counter doesn't work for the rest of the letters and always print "0". This is what I did:
program read_file
implicit none
integer :: count, i, j, k, n
character(len=80) :: arch
character(len=128) :: line
character :: c, d
logical :: rexist
print *,'Name of the file (example: "text1.txt"): '
read *,arch
count=0
inquire(file=arch, exist=rexist)
if(rexist) then
open(unit=10, file=arch)
j=97
do i=65,90
c=achar(i)
d=achar(j)
do
read(unit=10, fmt='(A128)', end=999)line
n=len_trim(line)
do k=1,n
if(line(k:k)==c .or. line(k:k)==d)then
count=count+1
end if
end do
cycle
999 exit
end do
print *, "Letter ", c, " or ", d, " Total: ", count
j=j+1
count=0
end do
close(unit=10)
else
print *,"Invalid file!"
end if
end program read_file
The first count works fine (letter A or a) but it prints "0" for the rest of the letters. Is there something wrong with the DO loop that doesn't reset the variable count properly?

Removing whitespace in string

I have the following code:
program main
character (len=15) :: abc = "te st tex t"
print *, trim(abc)
end program main
Which outputs:
te st tex t
I excepted all the whitespace to be removed but it wasn't. How can I remove all the whitespace from the string?
Trim will remove spaces only at the edges, not in the middle (this is common behaviour on almost all languages/libraries). If you want to remove all spaces in the string, you will have to create your own function to do this, iterating through the string.
Ex.:
program Test
implicit none
! Variables
character(len=200) :: string
! Body of Test
string = 'Hello World 7 9'
print *, string
call StripSpaces (string)
print *, string
contains
subroutine StripSpaces(string)
character(len=*) :: string
integer :: stringLen
integer :: last, actual
stringLen = len (string)
last = 1
actual = 1
do while (actual < stringLen)
if (string(last:last) == ' ') then
actual = actual + 1
string(last:last) = string(actual:actual)
string(actual:actual) = ' '
else
last = last + 1
if (actual < last) &
actual = last
endif
end do
end subroutine
end program Test
This was tested on intel compiler, not on gfortran, but I think it will work.
I was able to do this using the variable string library described here ( http://schonfelder.co.uk/is1539-2-99.htm ). The source code link is found in the introduction section of the ISO document.
Here is the code
program Console1
use ISO_VARYING_STRING
implicit none
! Body of Console1
character(LEN=50) :: text = 'Hello World John Mary '
character(LEN=50) :: res
print *, trim(text)
! 'Hello World John Mary'
res = REPLACE(text,' ','', every=.TRUE.)
print *, trim(res)
! 'HelloWorldJohnMary'
end program Console1
Here's a dirty, shameful way to eliminate the spaces. This is only likely to work if a compiler lays out a length-15 string in the same order and space as it would a 15-element array of characters. While this is likely to be true, and in my recent experience is true, it is not guaranteed to be so by the standard. That aside, this approach may be good enough.
! declarations
CHARACTER (len=15) :: abc = "te st tex t"
CHARACTER, DIMENSION(LEN(abc)) :: abc_array
! or CHARACTER, DIMENSION(:), ALLOCATABLE :: abc_array if your compiler supports
! automatic allocation
! transfer the string into an array of characters
abc_array = TRANSFER(abc,abc_array)
! eliminate the spaces, and transfer back to the string
abc = TRANSFER(PACK(abc_array,abc_array/=' '),abc)
! now all the spaces are at the end of abc so the following statement writes the
! string with no spaces
WRITE(*,*) TRIM(abc)
Use this approach at your own risk.
For those averse to TRANSFER perhaps a nice little recursive function would appeal. As written this depends on Fortran 2003's ability to automatically allocate character scalars, but it shouldn't be too hard to modify if your compiler doesn't support this feature yet.
RECURSIVE FUNCTION stripper(string,ch) RESULT(stripped)
CHARACTER(len=*), INTENT(in) :: string
CHARACTER, INTENT(in) :: ch
CHARACTER(:), ALLOCATABLE :: stripped
IF (LEN(string)==1) THEN
IF (string==ch) THEN
stripped = ''
ELSE
stripped = string
END IF
ELSE
IF (string(1:1)==ch) THEN
stripped = stripper(string(2:),ch)
ELSE
stripped = string(1:1)//stripper(string(2:),ch)
END IF
END IF
END FUNCTION stripper
You can try this:
program test
!erase blank space in a string
!run over every character of the string and just take every non-blank in other variable.
implicit none
character (len=100) str1,str2
integer i
str2='' !in this variable will be save non-blank spaces
str1=' a b c de ' !Test string with blank spaces
write(*,*)len_trim(str1), str1
do i=1,len(str1)
if (str1(i:i).ne.' ')str2=trim(str2)//trim(str1(i:i))
end do
write(*,*)len_trim(str2), str2
end

Fortran - String with unknown characters into substrings

I am trying to put an input string into sub-string arrays. The number of data in the input file are less than 10 but unknown. The number of spaces between each data is also unclear.
Example:
Asd B Cwqe21 Ddsw Eww
I am quite novice to Fortran, so I do not know which format I should use. My problem is that I do not know the number of data (here I assumed that there are 5), so how can I make the code work?
I tried the following which did not work:
CHARACTER (LEN=100), DIMENSION(10) :: string
READ (1,*) (string,I=1,10)
It seems that the error I got was because there was no 6th string to read and put into string(6).
I tried using the "Index" to find the space, but since I do not know how many spaces are in the string, it did not help me.
I don't know if this is more or less elegant/efficient than the standard approach in M.S.B's comment, but an interesting alternative.
integer istart,nw
character (len=100) line,wd,words(100)
open(1,file='t.dat')
read(1,'(a)')line
istart=1
nw=0
do while(len(trim(line(istart:))).gt.0)
read(line(istart:),*)wd
istart=istart+index(line(istart:),trim(wd))+len(trim(wd))
nw=nw+1
words(nw)=trim(wd)
enddo
write(*,*)trim(line)
write(*,*)('/',trim(words(k)),k=1,nw),'/'
end
An inefficient approach that is simple to program is to try to read the maximum number of items, and if this fails to successively try to read one fewer items until the read is successful, as shown below:
program xread_strings
integer, parameter :: nw = 10
character (len=1000) :: text
character (len=20) :: words(nw)
integer :: i,ierr,nread
text = "Asd B Cwqe21 Ddsw Eww"
nread = 0
do i=nw,1,-1
read (text,*,iostat=ierr) words(:i)
if (ierr == 0) then
nread = i
exit
end if
end do
if (nread > 0) write (*,*) "read ",nread," words: ",("'"//trim(words(i)) // "' ",i=1,nread)
end program xread_strings
! g95 Output:
! read 5 words: 'Asd' 'B' 'Cwqe21' 'Ddsw' 'Eww'