Physics Forums

Physics Forums (http://www.physicsforums.com/index.php)
-   Programming & Computer Science (http://www.physicsforums.com/forumdisplay.php?f=165)
-   -   Fortran write (or print). How to controll "new line" or not (http://www.physicsforums.com/showthread.php?t=256503)

uart Sep15-08 12:51 PM

Fortran write (or print). How to controll "new line" or not
 
I haven't touched fortran for about 100 years (and that's only a very slight exaggeration). Recently I had to write a very small amount of code in Fortran 95. I've picked up most aspects pretty quickly but I'm still a bit vague on formating output (using either print or write).

One thing I can't figure out is how to control whether or not something is writen on a new line or not. In the limited trial and error tests I've done it seems that (at least with numerical arguments) printing multiple variables in the one print statement puts them on the same line if I use the "generic" format string '*' and puts them on seperate lines if use a specific format string, like "(f8.3)" for example.

I'm assuming that there is some way to control where and when print (or write) takes a new line. Can someone help me out.

thanks.

mgb_phys Sep15-08 02:03 PM

Re: Fortran write (or print). How to controll "new line" or not
 
Back when I did fortran ( which involved the hull design on the Ark!) you put $ at the end of the format statement, I think in fortran90 there is "advance='no'" as well, not sure if the $ still works.

uart Sep15-08 08:48 PM

Re: Fortran write (or print). How to controll "new line" or not
 
Quote:

Quote by mgb_phys (Post 1874583)
Back when I did fortran ( which involved the hull design on the Ark!) you put $ at the end of the format statement, I think in fortran90 there is "advance='no'" as well, not sure if the $ still works.

The Ark, hehe good one :D

Anyway that "advance=" specifier was what I was looking. The form of the write statement is,

"write (file_number,format_spec,advance='no') variable_name"

It's still a bit quirky, here's what I've found.

- If you use the default format specifier * then it wont let you use advance='no', it compiles but gets a run-time error "advance not allowed here".

- But if you use the defualt format * then it automatically writes multiple variables (in a single write staement) on the same line if possible. As in "write (*,*) a,b,c", variables a,b and c are all written on the same line. However since you cant use the advance specifier here (for some unknown reason) then you have no option about what the next write statement will do, it will always use a new line.

- But if you dont use the default format *, and instead specify a format string like "(f8.3)" for example, then it wont write multiple variables to the same line.
For example "write (*,"(f8.3)") a,b,c" will write variables a,b and c each to a new line.

- If you add the advance='no' specifier then it only effects whether of not the next write statemnt starts a new line.
So for example write (*,"(f8.3)",advance='no') a,b,c still writes a,b and c on different lines, but the next write statement will start it's output on the same line a "c" was written.

Dam this is quirky and horrible. :(

alphysicist Sep15-08 09:45 PM

Re: Fortran write (or print). How to controll "new line" or not
 
Quote:

Quote by uart (Post 1875047)
The Ark, hehe good one :D

Anyway that "advance=" specifier was what I was looking. The form of the write statement is,

"write (file_number,format_spec,advance='no') variable_name"

It's still a bit quirky, here's what I've found.

- If you use the default format specifier * then it wont let you use advance='no', it compiles but gets a run-time error "advance not allowed here".

- But if you use the defualt format * then it automatically writes multiple variables (in a single write staement) on the same line if possible. As in "write (*,*) a,b,c", variables a,b and c are all written on the same line. However since you cant use the advance specifier here (for some unknown reason) then you have no option about what the next write statement will do, it will always use a new line.

- But if you dont use the default format *, and instead specify a format string like "(f8.3)" for example, then it wont write multiple variables to the same line.
For example "write (*,"(f8.3)") a,b,c" will write variables a,b and c each to a new line.

Did you want them on the same line? As you have it written it's coming to the end of the specifier list while you still have values to output. For them all on the same line, you might try:

Code:

write (*,"(3f8.3)") a,b,c
and to have two writes on the same line you can use, for example:

Code:

write (*,"(3f8.3)",advance="no") a,b,c
write (*,"(3f8.3)") d,e,f


uart Sep16-08 08:44 AM

Re: Fortran write (or print). How to controll "new line" or not
 
Quote:

As you have it written it's coming to the end of the specifier list while you still have values to output.
Yes alphysicist that fixed it, thanks.

I may have misread the documentation because I thought it said that if it got to the end of the format specifier and there were still items in the list then it just repeated from the beginning of the format specifier. Anyway that's what it seemed to be doing because it was printing each of the 3 variables as per the given format specifier, just not putting them on the same line. Anyway doing it properly as you've indicated with the count prefix works nicely. At last I feel in control of my output, yeah!!!!

BTW. It's really interesting to look at Fortran after all these years. It's changed so much and adopted so many things from other languages, I got a bit of a surprise really. My recollections of Fortran were pretty bad, but Fortran95 (or later) actually looks like it would be quite a nice language for handling number crunching stuff. I especially love the inclusion of complex numbers as an intrinsic data type with all the mathematical functions natively handling complex argument. I also really like the matlab style of handling array ranges and sub-ranges.

alphysicist Sep16-08 10:53 AM

Re: Fortran write (or print). How to controll "new line" or not
 
Quote:

Quote by uart (Post 1875618)
Yes alphysicist that fixed it, thanks.

I may have misread the documentation because I thought it said that if it got to the end of the format specifier and there were still items in the list then it just repeated from the beginning of the format specifier. Anyway that's what it seemed to be doing because it was printing each of the 3 variables as per the given format specifier, just not putting them on the same line. Anyway doing it properly as you've indicated with the count prefix works nicely. At last I feel in control of my output, yeah!!!!

You're right about it repeating from the beginning (though how it repeats is a bit more complicated if your format specifier list has nested parenthesis). However, fortran assumes that when you come to the end of a specifier list that you want to start a new line. For example, the line I posted could be written as either of these two equivalent lines:

Code:

write (*,"(3f8.3)") a,b,c
write (*,"(f8.3,f8.3,f8.3)") a,b,c

but if you compare that to the ouput of either of these two equivalent lines:

Code:

write (*,"(2f8.3)") a,b,c
write (*,"(f8.3,f8.3)") a,b,c

you can see how it handles the repeating.



Quote:


BTW. It's really interesting to look at Fortran after all these years. It's changed so much and adopted so many things from other languages, I got a bit of a surprise really. My recollections of Fortran were pretty bad, but Fortran95 (or later) actually looks like it would be quite a nice language for handling number crunching stuff. I especially love the inclusion of complex numbers as an intrinsic data type with all the mathematical functions natively handling complex argument. I also really like the matlab style of handling array ranges and sub-ranges.
I definitely think it's great for calculations--especially how it handles arrays, which is a marked improvement in fortran95. (My Fortran experience has been the opposite of yours--I started on fortran 77, and since then have used the language steadily. When fortran 90 and then fortran 95 came out, there was not a sudden transition for me. I just kept writing mainly fortran 77 style programs (except in free form) and slowly over time I used more and more of the new features until they all seemed natural.)

mgb_phys Sep16-08 10:59 AM

Re: Fortran write (or print). How to controll "new line" or not
 
I haven't used it seriously since the F77 days but it does have afew advantages over c++. Because it can guarantee some pointer operation isn't going to change an array the compiler can automatically parallize a lot of code which is going to start being important for the multicore CPUs we are being promised - it might start to make a come back.

MareRobins Aug3-11 02:08 PM

Re: Fortran write (or print). How to controll "new line" or not
 
Hi all,

I am writing to text files (with Fortran 77), and I would prefer to use just * for format so I don't have to get specific with every column in every file.
I thought that I understood that if you:
write(12,*) a(i), b(i), c(i)
then each time it hits the write statement it will create a new line, but during a given print statement it will not advance a line on its own.

I have some very long lines of data (10-15 columns of data) and it seems to be automatically advancing a line in the middle of that data.

Do you have any suggestions to maintain the auto format (or not have to set it to rounding decimal places with F) but keep all columns in a single write statement on one line?

Many thanks!
Mare


All times are GMT -5. The time now is 01:40 PM.

Powered by vBulletin Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2014 Physics Forums