|
|||
|
On Friday, 22 June 2012 07:35:10 UTC+10, glen herrmannsfeldt wrote:
It beggars belief that, after 55 years, it still is necessary to resort to hacks in Fortran to do simple things like a variable number of values on a line and/or a variable field width per element. While it currently isn't necessary to resort to the hacks described below, what is still necessary (via internal I/O as described elsewhere in the 25+ replies in this thread in comp.lang.fortran) is still causing endless grief and discussion. In PL/I that kind of I/O is done trivially because the repeat factor can be an expression. Thus, in PL/I, row i of an array A can be printed using put edit (A(i,*)) ( skip, (n)F(10,3) ); where variable n gives the number of values required on each line. To vary the field width, one uses a variable instead of a constant: put edit (A(i,*)) ( skip, (n)F(k,3) ); where k is a variable whose value might be anything from, say, 4 to 15. > Richard Maine <nospam@see.signature> wrote: > > Nasser M. Abbasi <...@12000.org> wrote: > > (snip) > >> > write(*,FMT) ((B(i,j),j=1,nCol),i=1,nRow) > > >> I actually tried the above, but overlooked the need > >> for the () there. I was writing > (snip) > > > I certainly understand making that error. It is one I made myself when > > first using character strings for formats. That was over 30 years ago > > (basically when f77 compilers started being available), and I've learned > > that one by now, but I still recall it as being an error I once made. > > I remember using variable formats back to the F66 days, and > don't remember making (or not making) that error. > > Well, that was before internal I/O, so had to use other ways > to put together a FORMAT string. We did have FIO999 to allow > for internal I/O (another story) but I think I did it putting > together array elements. > > The only one I remember used REAL*8 variables so I could fit > eight characters in each, big enough for whole format strings. > In the OP's case, I might have done: > > REAL*8 FMTS(3)/'(1I5)','(2I5)','(3I5)'/ > > (Also using a compiler extension allowing initialization on > the declaration statement.) > > Then select the appropriate format. > > REAL*8 FORMAT(1) > > FORMAT(1)=FMTS(N) > WRITE(6,FORMAT) (A(I),I=1,N) > > But I knew the 9999 trick by then, so it might have been: > > REAL*8 FMTS(3)/'(F10.1)','(F10.2)','(F10.3)'/ > > To allow selecting the width at run time. For actual multi-digit > numbers, one could do something like: > > INTEGER DIGITS(10)/'0','1','2','3','4','5','6','7','8','9'/ > > INTEGER FORMAT(7)/'(E','','','.','','',')'/ > > then assign elements of DIGITS to FORMAT(2), FORMAT(3), > FORMAT(5) and FORMAT(6) > > Which allows one to put together enough characters for most > types of dynamic format strings one might need, though not quite > as easily as with internal I/O. |
|
|
||||
|
||||
|
|
|
|||
|
On Monday, 25 June 2012 18:53:33 UTC+10, glen herrmannsfeldt wrote:
> rob.......@gmail.com wrote: > > > It beggars belief that, after 55 years, it still is necessary to > > resort to hacks in Fortran to do simple things like a variable number > > of values on a line and/or a variable field width per element. > > > While it currently isn't necessary to resort to the hacks described below, > > what is still necessary (via internal I/O as described elsewhere in the > > 25+ replies in this thread in comp.lang.fortran) is still causing endless > > grief and discussion. > > > In PL/I that kind of I/O is done trivially because the repeat factor > > can be an expression. > > Reminds me, though, that, at least last I knew, PL/I doesn't > have a completely variable format form that Fortran has allowed > since Fortran 66, and C since the beginning of C. That is, > the ability to read in a format string and use that. It wasn't provided, for the very good reason that the compiler can't check a format specification that's read in. In FORTRAN, it was a problem, for if the specification was wrong, FORTRAN just crashed. Instead, the ability to specify -- as expressions -- a repeat factor, field width, and decimal places was provided, and this is checked by the compiler. The ability to do that imparts considerable flexibility and many benefits. See also below. > > Thus, in PL/I, row i of an array A can be printed using > > put edit (A(i,*)) ( skip, (n)F(10,3) ); > > where variable n gives the number of values required on each line. > > > To vary the field width, one uses a variable instead of a constant: > > put edit (A(i,*)) ( skip, (n)F(k,3) ); > > where k is a variable whose value might be anything from, say, 4 to 15. > > The ability to generate such using internal I/O is pretty easy, but > generating a completely arbitrary format from program input in PL/I > is very difficult. It's trivial in PL/I. The EDIT function provides the means to format a data value according to any string read in. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|