Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

iterate on the name of an output file

12 views
Skip to first unread message

pluton

unread,
May 6, 2013, 9:11:51 PM5/6/13
to
Hello,

in the loop below, the A[k] are plots involving simple curves previously calculated. I would like to export the numerical values associated to each of these plots in external text files. Here is what is done:

for k from 1 to 10 do:
B:=op(1,op(1,A[k])):
for i from 1 to nops(B) do:
myline:=sprintf("%10.6f\t%10.6f",B[i,1],B[i,2]):
writeline("data[k].dat",myline):
end do:
close("data[k].dat"):
end:

The problem is that I want to iterate on the names of the output files, ie data[k].dat. In the above example, I want to write in the files data1.dat, data2.dat, ... data10.dat while the provided code only writes in data[k].dat. How can this be achieved?

Thank you

acer

unread,
May 6, 2013, 9:27:49 PM5/6/13
to
How about changing one line to,

writeline(cat("data",k,".dat"),myline):

Or you could use `sprintf`.

pluton

unread,
May 6, 2013, 9:46:33 PM5/6/13
to
interesting, the file names are correctly incremented but the files are empty?
What would be the solution with sprintf? Thank you again.

pluton

unread,
May 6, 2013, 9:48:45 PM5/6/13
to
sorry, that is perfectly fine. I forgot to close the files. It works well. Thank you.
0 new messages