Silverfrost Forums

Welcome to our forums

Unlimited repeat count on Format statement

10 Mar 2013 9:44 (Edited: 2 Apr 2013 4:16) #11700

When you need to repeat a Format sequence a number of times, but don't know how many, it is traditional to use a large number, such as in the following example.

write(*,'(9999(f7.3,:,1x))') a(1:number)

In such code, the repeat counter 9999 is chosen to be larger than any possible value of number. (The : is used to prevent a space being printed after the last value).

It would be nice to allow the following extension from Fortran 2008 for this case.

write(*,'(*(f7.3,:,1x))') a(1:number)

Just a suggestion 😉

11 Mar 2013 6:59 #11705

Nice idea.

11 Mar 2013 7:30 #11717

So you are trying to teach computer what infinity means 😃

DEC Fortran had nice extension which still works in CVF/IVF which is even better

write(*,'(<number>(f7.3,:,1x))') a(1:number)

where value in <> parentheses could be not a fixed number but a variable where you can put whatever you want because it like all variables of course could be changed dynamically

15 Mar 2013 11:15 #11780

The problem with the DEC/Intel extension is you need to include the count variable twice on the same line. The * method is just a bit more concise and is part of the Fortran standard. It has also been implemented in the Intel compilers as an alternative to the DEC extension.

17 Mar 2013 9:52 (Edited: 17 Mar 2013 11:06) #11798

Depends of what you are doing. The <> is absolutely intuitive and is based on common sense that all must be changeable at run time, including super-conservative format statement. It perfectly succeeded to make nice and elegant code for run-time changeable formats, all it is done with absolute minimum of typing leading to very readable code

Write(*,'(T<variable>, i10)') something
Write(*,'(E<variable1>.<variable2>)') something
Write(*,'(I<variable>)') something
Write(*,'(<variable>X, E10.3)')  something

Possibilities of <> are infinite and all are extremely useful (versus this unlimited repeat format for which, sorry, i see just one). I miss <> in this compiler very much, i am doing similar things now in several lines using character variable as a format filled using internal write....yuck

17 Mar 2013 10:10 #11800

Yes, these are good examples.They are mostly not repeat counters though, which was the point of this suggestion.

If you really need to have this functionality, you can do it without using the DEC extensions in standard Fortran.

For example:

character*1 :: num_size, num_decimals
num_size = '5'
num_decimals = '3'
write(*,'(F'//num_size//'.'//num_decimals//')') 2.59
end

Most of the time, you will need to use TRIM, which can be a bit messy.

17 Mar 2013 10:17 #11801

LOL If i knew that 5 and 3 in advance i'd just write

write(*,'(F5.3)') 2.59

In practice your code will look

character*1 :: num_size, num_decimals 
write(num_size,'(i1)')  variable1
write(num_decimals,'(i1)') variable2
write(*,'(F'//num_size//'.'//num_decimals//')') 2.59 
end

versus

write(*, '(F<variable1>.<variable2>')) 2.59 
17 Mar 2013 1:09 #11804

Obviously, it was just an example.

Please login to reply.