Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29393 Discussions

using an integer variable in a format statement

Anonymous44
Beginner
1,318 Views
I've had a hard time finding this information and don't even know if it is possible. I'd like to use a format statement, in which spacing will be variable. For example,I'd like to usean integer variable 'nspacer' that would be put in a format statement that will allow me to change the spacing without explicitly writing out the number.
100 format('nspacer'x,'blah blah blah')
Is this possible and if so, what is the type of format (I know the above is wrong) I should use? Ifanyone canhelp me out, I'd really appreciate it. Forgive my ignorance.
Thanks,
CN
0 Kudos
5 Replies
anthonyrichards
New Contributor III
1,318 Views
Here is an example of what you can do.....
Code:
!  VARIABLEFORM.f90 
!
!  FUNCTIONS:
! VARIABLEFORM      - Entry point of console application.
!
! Example of displaying 'Hello World' at execution time.
!
!****************************************************************************
!
!  PROGRAM: VARIABLEFORM
!
!  PURPOSE:  Entry point for 'Hello World' sample console application.
!
!  A variable format is constructed in the character array VARFORMAT
! this is used to control a WRITE to the console
! one element of the array will contain an integer multiplier for the
! 'space' indicator 'X'.
!
!****************************************************************************

PROGRAM VARIABLEFORMAT
IMPLICIT NONE
character*8 varformat(8)
character*8 openbracket, closebracket, comma, xspace, blanks, BLAH1, BLAH2
CHARACTER*8 multispace
character*100 vformat
integer*4 ispaces, i, j, istart, iend, length
DATA BLANKS/8H        /
DATA BLAH1, BLAH2/8H'HELLO' ,  8H' WORLD'/
! define parts used to make up the format statement
DATA OPENBRACKET, CLOSEBRACKET,COMMA, XSPACE/8H(1H ,   ,   8H )       ,   8H,       ,  8HX       /
VFORMAT=ADJUSTL(ADJUSTR(VFORMAT))   ! Initialises character variable VFORMAT to all blanks...
! Initialise each element of character array VARFORMAT to contain blanks
DO I=1,8
VARFORMAT(I)=BLANKS
ENDDO
! construct a sequence of 5 variable formats, increasing the space multiplier by 5 each time
DO I=1,5
 VARFORMAT(1)=OPENBRACKET
 ISPACES=5*I
 WRITE(MULTISPACE,'(I8)') ISPACES    !Converts integer in ISPACES to character-string in MULTISPACE
 VARFORMAT(2)=BLAH1
 VARFORMAT(3)=COMMA
 VARFORMAT(4)=MULTISPACE
 VARFORMAT(5)=XSPACE
 VARFORMAT(6)=COMMA
 VARFORMAT(7)=BLAH2
 VARFORMAT(8)=CLOSEBRACKET
! concatenate all the parts of the format into a single character variable
! so that it can be output....each added part is 8 characters long
  DO J=1,8
  VFORMAT(1+(J-1)*8:8+(J-1)*8)=VARFORMAT(J)(1:8)
  ENDDO
 WRITE(6,'(A100)') VFORMAT        ! Write the whole variable format out....
 WRITE(6,varformat)    !Then use the variable format...
ENDDO
end program VARIABLEFORMAT

Message Edited by anthonyrichards on 08-16-2004 05:56 AM

0 Kudos
aliho
Beginner
1,318 Views
You can also do as follow:

character(lenmax) spaces

spaces =" "

write(..,100)spaces(1:nspaces)
100 format(a,,'blah blah blah')
0 Kudos
fobermaier
Beginner
1,318 Views
why don't you use the angle brackets within your format expression:
100 format(x, 'blah, blah blah')
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,318 Views
As a variation on Aliho's idea, let me also suggestthe following(brackets are simple but non-standard, internal write is standard but complicated):
Code:
write(*, "(A, BLAH BLAH BLAH)") REPEAT(" ",nSpaces), blah, blah, blah
Jugoslav

Message Edited by JugoslavDujic on 08-16-2004 04:15 PM

0 Kudos
Anonymous44
Beginner
1,318 Views
Thank you all for your help. Great ideas!
0 Kudos
Reply