Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove leading space when printing with write?

Suppose I have the following code

program fortran
  open(900, FILE='SOMETHING')   
  write(900, *) '21'    
end program fortran

The file form will be

 21

that is, there is a space before the number. How to get rid of that space?

like image 772
user3892863 Avatar asked Jul 30 '14 18:07

user3892863


People also ask

How do you remove leading spaces in SAP?

To Remove Leading Spaces in Character Field We Can Use SHIFT ----LEFT--- DATA V_CURR(20) VALUE ' Test string to check shift ', length TYPE I. SHIFT V_CURR LEFT DELETING LEADING SPACE.

How do you remove leading and trailing spaces in SAP ABAP?

ABAP keyword CONDENSE is useful to remove leading and trailing space characters from a character string, however CONDENSE does not recognize other non-printing whitespace characters such as carriage returns, newlines and horizontal tabs.


1 Answers

You can write it as a string:

PROGRAM fortran 

  OPEN(900,FILE='SOMETHING')
  WRITE(900,'(a)') '21'

END PROGRAM FORTRAN
> cat SOMETHING 
21

To respond to the comment:

The more explicit way of doing that would be to write the number into a string (you could also use list-directed I/O for this step), remove whitespaces from the string trim and finally output the left-adjusted adjustl:

program test
  character(len=23) :: str

  write(str,'(ES23.15 E3)') 1.23d0
  write(*,'(a)') adjustl(trim(str))

  write(str,'(ES14.7 E2)') 0.12e0
  write(*,'(a)') adjustl(trim(str))
end program
> ./a.out 
1.230000000000000E+000 
1.2000000E-01

This solution is probably more complicated then necessary, but it is a very flexible approach that can be extended easily for arbitrary purposes and formats.

like image 169
Alexander Vogt Avatar answered Sep 19 '22 15:09

Alexander Vogt