Re: how to write formatted output <n>f10.5
From: James Van Buskirk (not_valid_at_comcast.net)
Date: 01/09/04
- Next message: Mark Yudkin: "Re: PL/I CAN 1"
- Previous message: Jason Nielsen: "Re: how to invoke intel debugger in kdbg?."
- In reply to: Mag: "Re: how to write formatted output <n>f10.5"
- Next in thread: Mag: "Re: how to write formatted output <n>f10.5"
- Reply: Mag: "Re: how to write formatted output <n>f10.5"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 09 Jan 2004 07:55:31 GMT
"Mag" <geohydrol@yahoo.com> wrote in message
news:be634860.0401082132.f0f1673@posting.google.com...
> Thank you very much, James!
> I got the solution thanks to you, but I would very much appreciate if
> you give some explanation about what did you do in the following line?
> Just a quest to know the process so that I can use the method for
> other similar problems.
> write(fmt,'(a,i0,a)') "(1x,",ninp+1,"('''',a,''', '),'''',a,'''')"
Well, I wanted to write exactly the iolist you specified in exactly
the format you specified. To accomplish a feat like this I usually
take off my physicist's hat and put on my chemist's hat: just as in
chemistry you often want to start from the target molecule and work
backwards, here we want to start from the desired output and derive
a format that will do the trick.
If the character expressions you wanted to print out were, in order,
'a '
'b'
'cc'
'ddd'
'eeee'
(without the quotes) then your desired output would be:
'a ', 'b', 'cc', 'ddd', 'eeee'
We want that space at the front to guard agains the possibility that
the unit we output it to may have CARRIAGECONTROL = 'FORTRAN'.
Now, all the strings save the last are going to be output with a
leading apostrophe, the string, a trailing aposthrophe, then a comma
and space. If we replaced the apostrphes with number signs, the
format for these would be:
'#',a,'#, '
but we want apostrophes... so how do you represented apostrophes
withing a string delimited by apostrophes? Well, you can dodge the
issue by delimiting the string by quotation marks instead, but this
creates problems because everything will have to be inside another
string to the statement that composes our format. It will simplify
our comprehension of the code if we reserve quotation marks for that
string and use apostrophes for everything else. Try solving the
problem all eight ways using quotation marks or apostrophes at each
level of quotation and you may see what I mean.
In Fortran, to include an apostrophe within an apostrophe-delimited
string, we write instead two consecutive apostrophes. Thus, to change
the number signs to apostrophes, we change:
'#',a,'#, '
to:
'''',a,''', '
The last string should be output as only a leading apostrophe, the
string, then a trailing apostrophe. Changed to number signs this
format would read:
'#',a,'#'
Changing back to apostrophes:
'''',a,''''
In the case we are contemplating, we need the leading blank, 4 of
the first kind of formats, and only one of the last kind:
1x
4('''',a,''', ')
'''',a,''''
Putting this together, surrounding with parentheses and delimiting
with commas to make it all a valid format string we get:
(1x,4('''',a,''', '),'''',a,'''')
We are almost done, but the number of times the first kind of format
is repeated, here 4, is in general ninp+1. We write what precedes
it and what follows it as strings via the a format, and the repeat
count itself as an integer with the i0 format which acts for integers
kind of like the a format works for strings: it writes exactly the
number of characters needed to represent the integer. Thus we
arrive at the WRITE statement that composes the format string in
question.
BTW, what happens if you have to compose a format string that will
control the output of a character expression that will be read via
list-directed formatting and then used in a format expression that
itself contains apostrophe-delimited strings? In F77 where no
quotation-mark delimiting is allowed? At each level of parsing,
the number of apostropes doubles; in one case I had to use blocks
of 32 apostrophes, and the text editor I was using didn't have a
column counter in the lower right-hand corner, so I had to count
the !@#$%^& things by hand... if frequent readers of this forum
find that I tend to avoid character strings in format statements
and format strings like the plague and also avoid list-directed
input of character variables when feasible, perhaps they may offer
more sympathy for my eccentricities.
If I had my druthers, I would define a parameter to make it easier
to express an apostrophe and then change my iolist so that each
string comes already surrounded by apostrophes. Replace:
string_expression
with:
c//string_expression//c
where c is a parameter containing the single character, apostrophe.
Then the format expression from the example above could be:
(1x,5(a:', '))
The colon above acts like a comma except that it suppresses the
output of the final comma and space because the iolist would be
used up at this point (try changing it to a comma to see the
difference.) The extra parameter declaration and the two modified
lines of code would then read:
character, parameter :: c = "'"
write(fmt,'(a,i0,a)') '(1x,',ninp+2,"(a:', '))"
write(*,fmt)
c//datec//c,c//trim(outputc)//c,(c//trim(inputc(i))//c,i=1,ninp)
(Sorry for any line-wrap above)
HTH.
-- write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, & 6.0134700243160014d-154/),(/'x'/)); end
- Next message: Mark Yudkin: "Re: PL/I CAN 1"
- Previous message: Jason Nielsen: "Re: how to invoke intel debugger in kdbg?."
- In reply to: Mag: "Re: how to write formatted output <n>f10.5"
- Next in thread: Mag: "Re: how to write formatted output <n>f10.5"
- Reply: Mag: "Re: how to write formatted output <n>f10.5"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|