Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

several outputs on the same line

haminin
Beginner
6,219 Views

Hi there

Could someone let me know how to write several times free-format outputs on the same line.

Many thanks

Hamid

0 Kudos
26 Replies
Peter
Beginner
5,239 Views
Quoting - haminin

Hi there

Could someone let me know how to write several times free-format outputs on the same line.

Many thanks

Hamid

Can you please be more specific?? Like an example on what you are trying to do??
0 Kudos
Peter
Beginner
5,239 Views
try this code

program main
integer*8 a,b,c
real*8 u,v,w
data a,b,c,u,v,w/1,2,3,4.5678,3.42145,2.78934/
write(*,10) a,b,c,u,v,w
10 format('Integers=',3(1x,i5),2x,'real numbers=',3(2x,f6.4))
end program

Is this what you are looking for?

Hope it helps
0 Kudos
haminin
Beginner
5,239 Views
Quoting - peter
Can you please be more specific?? Like an example on what you are trying to do??

Thank you peter.

In the following example, the program prints 10 times the inputted integer number.

integer I, a

5 read*,a

do I = 1, 9

write (*, 10, advance='no') a

enddo

! New line to complete

write (*, 10) a

10 format (9I2)

go to 5

stop

end

But how can I do thatwith a free format writing?
Hamid

0 Kudos
Peter
Beginner
5,239 Views
simple add a free format .f90,.F90,.i90 file to your project.
this can be done by
project>add new item>fortran file(.f90)

when i use your code i get results as follows
1
1 1 1 1 1 1 1 1 1
2
2 2 2 2 2 2 2 2 2

is this what you are looking for?

0 Kudos
haminin
Beginner
5,239 Views
Quoting - peter
simple add a free format .f90,.F90,.i90 file to your project.
this can be done by
project>add new item>fortran file(.f90)

when i use you code iget results as follows
1
1 1 1 1 1 1 1 1 1
2
2 2 2 2 2 2 2 2 2

is this what you are looking for?


Yes it is, but I get each one ten times.

When I said free format, I meant using print *,or write(*,*).

How can I add the free format in Visual Studio?

0 Kudos
Peter
Beginner
5,239 Views
try doing this

program main
integer i,a,j
dimension a(9)
do i=1,9
read(*,*) a(i)
end do
do j=1,9
write(*,10) (a(i),i=1,9)
10 format(9(1x,i2))
end do
end


this is modern version of your code. by the way what compiler are you using?.try not to use 'go to' statements often. That hasbeen outdated.
unfortunately you have to use format statement to make you output more understandable.
that is why i've used format.

Hope this helps!
0 Kudos
haminin
Beginner
5,239 Views
Quoting - peter
try doing this

program main
integer i,a,j
dimension a(9)
do i=1,9
read(*,*) a(i)
end do
do j=1,9
write(*,10) (a(i),i=1,9)
10 format(9(1x,i2))
end do
end


this is modern version of your code. by the way what compiler are you using?.try not to use 'go to' statements often. That hasbeen outdated.
unfortunately you have to use format statement to make you output more understandable.
that is why i've used format.

Hope this helps!

Many thanks Peter, but you did not use free format writing!

The Program I posted was just a simple example.

In my main program I need to write many items on a line to input easilyinto a spread sheet. The thing is I need to use free format.

0 Kudos
Peter
Beginner
5,239 Views
I see what you are saying. I had this situation earlier.

Unfortunately or as far as i know , you have to use format statement when you want to analyse large data using excel or matlab. Because format statement controls your output. If it is free format 'fortan' decides what to do, but if you use 'format' you decide how the output should be.

However large the code be youmight wannabe comfortable geting used to 'format' statement, in my opinion.

I recommend you use matlab, if you have one and have been using it already!

Let me know if you have any questions
0 Kudos
haminin
Beginner
5,239 Views
Quoting - Peter
I see what you are saying. I had this situation earlier.

Unfortunately or as far as i know , you have to use format statement when you want to analyse large data using excel or matlab. Because format statement controls your output. If it is free format 'fortan' decides what to do, but if you use 'format' you decide how the output should be.

However large the code be youmight wannabe comfortable geting used to 'format' statement, in my opinion.

I recommend you use matlab, if you have one and have been using it already!

Let me know if you have any questions

The thing is that the number of the digits before and after the decimal point varies and that is why I am avoiding the F or E formats.

Thanks very much for your help but let's wait more as someone might know how to do thatwith free format.

0 Kudos
Peter
Beginner
5,239 Views
Quoting - haminin

The thing is that the number of the digits before and after the decimal point varies and that is why I am avoiding the F or E formats.

Thanks very much for your help but let's wait more as someone might know how to do thatwith free format.


I would be excited to see if someone gives an alternative option.

However you can control the number of decimal points. For example if you say f7.5 for a real(4) declaration, and lets say the number be 1.23456, the number of digits are 6, however if you want to get all you numbers after the decimal printed give f(6+1).5==f7.5, so you'l get output as 1.23456.

Again this is when you know the data that you input. If you are generating some values it is no harm to give extra width, say f10.5. I think you should spend some time playing with formats and be a good programmer.

0 Kudos
DavidWhite
Valued Contributor II
5,239 Views
Quoting - Peter
I see what you are saying. I had this situation earlier.

Unfortunately or as far as i know , you have to use format statement when you want to analyse large data using excel or matlab. Because format statement controls your output. If it is free format 'fortan' decides what to do, but if you use 'format' you decide how the output should be.

However large the code be youmight wannabe comfortable geting used to 'format' statement, in my opinion.

I recommend you use matlab, if you have one and have been using it already!

Let me know if you have any questions

If you output the numbers with CHAR(9) in between each one, you can produce a "tab-delimited" text file which Excel will easily read in and put all the values into the right cells.

0 Kudos
Peter
Beginner
5,239 Views
Quoting - David White

If you output the numbers with CHAR(9) in between each one, you can produce a "tab-delimited" text file which Excel will easily read in and put all the values into the right cells.

David,

I agree with you regarding char(9).This helpsuser to avoid 'format' statement.

The disadvantage I see is
1. Not compatible with type 'real'
Do you agree David ?


Haminin,

I still can not achieve print*, or flat 'write(*,*)' , because eventually, I believe, you have to write data into an external file.

In this context I used character(8) for printing numbers 1 - 9

[cpp]program main
integer i,j,a
character(8) f(9)
dimension a(9)
f=char(a(9))
do i=1,9
read(*,*) f(i)
end do
open(3,file='out.txt',status='unknown')
do i=1,9
write(3,*), (f(j),j=1,9)
end do
end[/cpp]
0 Kudos
haminin
Beginner
5,239 Views
Quoting - David White

If you output the numbers with CHAR(9) in between each one, you can produce a "tab-delimited" text file which Excel will easily read in and put all the values into the right cells.


But it goes to the next line after writing a few numbers.

0 Kudos
haminin
Beginner
5,239 Views
Quoting - Peter

David,

I agree with you regarding char(9).This helpsuser to avoid 'format' statement.

The disadvantage I see is
1. Not compatible with type 'real'
Do you agree David ?


Haminin,

I still can not achieve print*, or flat 'write(*,*)' , because eventually, I believe, you have to write data into an external file.

In this context I used character(8) for printing numbers 1 - 9

[cpp]program main
integer i,j,a
character(8) f(9)
dimension a(9)
f=char(a(9))
do i=1,9
read(*,*) f(i)
end do
open(3,file='out.txt',status='unknown')
do i=1,9
write(3,*), (f(j),j=1,9)
end do
end[/cpp]

It still goes to the nextline for example when "12" is used instead of "9".

0 Kudos
Les_Neilson
Valued Contributor II
5,239 Views

Haminin
There is some confusion.

char(a(9)) is not the same as char(9)

Youdon't really needto use the charactervariable f but if you do then it needs to be
character(1) ::f (that is one character)
f = char(9)

Unfortunately your READ statement then overwrites the contents of "f" ! I presume you meant to read into the array "a"
You then wrote the contents of "f" nine times - that is nine lines each with the same nine values stored in "f"


what you need is a write that will outputthe firstnumber followed by a char(9) (that is a TAB character) then the next number followed by a char(9), and so on.

do i = 1,n
write(*,*) a(i),char(9)
....

will outputa number followed by a TAB (you may use variable "f" in place of the char(9) if you have made the changes I describe above.

In order to output everything on one line you could write
write(3,*) ((a(i),char9)),i=1,9)

note if you only have an array of 9 items you don't need the surrounding do loop, the implied do loop in the write statement is enough.

but you will need to consider the line length of the output record
because advance="no" is not allowed with list directed format

Les


minor correction of terminology
what you call "free format" is actually "List Directed" format and is to do with I/O
"free format" would usually be understood as referring to source code
0 Kudos
haminin
Beginner
5,239 Views
Quoting - Les Neilson
but you will need to consider the line length of the output record
because advance="no" is not allowed with list directed format

Many thanks Les.

So it seems impossible to output several free-format data on one line.

Using write(3,*) ((a(i),char9)),i=1,9) does not print everything on one line as you said it depends on the line length.

Hamid

0 Kudos
Steven_L_Intel1
Employee
5,239 Views
List-directed output (what you call free-form) allows the "processor" to start a new record whenever it wants. In Intel Fortran, this is column 80 by default. You can change this by opening a unit and specifying a larger RECL= value.

I would agree with others that using list-directed output is not recommended when you care what the output looks like. An explicit format is much better for this and you will have full control over where lines begin. The G format item can be used for values of any type.
0 Kudos
DavidWhite
Valued Contributor II
5,239 Views
Quoting - haminin

Many thanks Les.

So it seems impossible to output several free-format data on one line.

Using write(3,*) ((a(i),char9)),i=1,9) does not print everything on one line as you said it depends on the line length.

Hamid

As Steve says in the next reply, change the RECL.

From experience, Excel uses double precision values (but with an E exponent), so I allow about 15 digits per value for Excel (The reason I do this is that I also export from Excel and read it back in using Fortran).

This means that allowing 15-20 characters per value, i.e. upto 180 for 9 values should be enough.

So this gives ...

OPEN(UNIT=3, FILE="MYFILE", RECL=180)
WRITE(3,*) ((A(i),CHAR(9)),i=1,9)

or similar.

Regards,

David

P.S., I can follow up with some of my actual code, but can't do this for about a week.
0 Kudos
Peter
Beginner
5,239 Views
Quoting - David White
As Steve says in the next reply, change the RECL.

From experience, Excel uses double precision values (but with an E exponent), so I allow about 15 digits per value for Excel (The reason I do this is that I also export from Excel and read it back in using Fortran).

This means that allowing 15-20 characters per value, i.e. upto 180 for 9 values should be enough.

So this gives ...

OPEN(UNIT=3, FILE="MYFILE", RECL=180)
WRITE(3,*) ((A(i),CHAR(9)),i=1,9)

or similar.

Regards,

David

P.S., I can follow up with some of my actual code, but can't do this for about a week.

David,

But the problem comes when, if you want to try printing 1-12

Can you please check this example code.
[cpp]program main
integer i,a,j
character(1) f
dimension a(12)
f=char(9)
do i=1,12
read(*,*) a(i)
end do
open(unit=3,file='out.txt',status='unknown',recl=1024)
do i=1,12
write(3,*) (a(i),char(9), j=1,11)
end do
end[/cpp]
I know there is nothing as good as using list directed output, but I still believe there must be a solution.

0 Kudos
Peter
Beginner
4,332 Views
Haminin,

This is a modification of my previous code. So now it prints 1-12 numbers on the same line. The output looks like this
1 1 1 1 ......
2 2 2 2 ......

This can be done by changing

[cpp]write(3,*) (f,a(i),j=1,11)[/cpp]
Let me know if this works for you!


0 Kudos
Reply