Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!elroy.jpl.nasa.gov!decwrl!world!ksr!tim From: tim@ksr.com (Tim Peters) Newsgroups: comp.lang.fortran Subject: Re: Implied do loop in write (SUMMARY) Summary: wouldn't hurt to read the standard Keywords: fortran, wonderful, obscure, nice output Message-ID: <3290@ksr.com> Date: 24 Apr 91 05:42:03 GMT References: <1991Apr23.201123.3908@unixg.ubc.ca> Sender: news@ksr.com Organization: Kendall Square Research Corp. Lines: 73 In article moshkovi@sanandreas.ecn.purdue.edu (Gennady Moshkovich) writes: > ... [stuff deleted] ... >>> do 1 i = 1,n >>> write(6,100) (matrix(i,j),j=1,m) >>> 1 continue >>>100 format(10i8) > ^^^^ > THIS IS THE PROBLEM !! > ... [stuff deleted] ... >Just look at the code, and you will see where the problem is. >You can't print with this code more then _10_ elements in a row. >I can repeat again !!!. I don't know beforehand how many >elements I have, I can have 3, or I can have 300, but the >output MUST look nice. Don't feel bad -- Fortran formats are very powerful but remarkably few programmers know how to use them. They're about as obscure as anything can be . If you've tried the "build the format at runtime" trick you've discovered it doesn't do what you really want either, so you'll be motivated to figure out why something like this does : parameter (m=50,n=50) integer a(m,n) do i = 1, m do j = 1, n a(i,j) = i + j end do end do do i = 1, m write(6,99) i,(a(i,j),j=1,n) end do 99 format('Row ',i4,': ',9i7/(10x,9i7)) end This gives the "nice" output: Row 1: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 Row 2: 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 ... output for 47 rows deleted ... Row 50: 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 The tricks here are to study the std to see how a "/" can be used to force a linefeed (= new "record"), and especially how nested parentheses in a format can be used to force a *part* of the format to be rescanned an arbitrary number of times. The latter trick is the crucial one here, and allows the format to work the way you want whether the rows happen to have one or a million elements. unfortunately-the-rules-are-hard-to-explain-in-a-few-lines-so-your-best- bet-really-is-to-study-the-standard-ly y'rs - tim Tim Peters Kendall Square Research Corp tim@ksr.com, ksr!tim@harvard.harvard.edu