Create multiple files with a loop

Good morning everyone

I have a question concerning Maple file output. Is there a way, that I can produce multiple files with Maple using a loop? My main problem is how to get Maple to create files with different names, like file1.txt -> file2.txt -> file3.txt ..... because so far i figured, that

fopen("file[i].txt",WRITE) , with i beeing the loop index

does not work :)

Thank you for any help

 

Karl

Axel Vogt's picture

string

in that case try to concatenate "file" + convert(i,string) + ".txt" to get a string as name

sprintf instead of catenation

The + sign doesn't catenate strings in maple, though possibly you weren't suggesting that.  The cat function does.  However, I prefer sprintf here:

for i to 3 do
  fopen(sprintf("file%d.txt", i),'WRITE');
end do;
John Fredsted's picture

Using sprintf

You could also write sprintf("file%d.txt",1), etc.

pointless

Hi John,

Your typing was quicker than mine, but I cleverly replied to the immediate poster rather than the original, so my response appears before yours.  We can split the single point. Congrats on winning the MaplePrime award, that wasn't a surprise.

It's Louis Riel day in Manitoba, so I'll be celebrating, even if I'm not there...

John Fredsted's picture

Smiling

Hi Joe,

It made me smile seeing that our posts had crossed each other. I guess I came before you, in time that is, because I typed less letters than you did.

Thanks for your words concerning the award. I had a maplesoft mug shipped (or rather flown, I reckon) all the way from Canada to the little kingdom of Denmark, in the flimsy hope that having coffee contained in a mug with the maplesoft name and logo on would somehow rub off on my performance when working with Maple and when attending this forum. The jury is still out on that issue; a little more data sampling is required. Obviously, it did not help this morning, compare the post Sleepy me.

What is Louis Riel day in Manitoba? Manitoba makes me think of a song by the Scottish band Runrig. Do you know them? They have a song referring to Manitoba. Cannot remember the title of that song, though.

Riel

Louis Riel was a significant figure in Canadian history (not to mention my own family history).  He led the Metis, a blend of native and French settlers, in a revolute against the Canadian government. He was one of the founders of the Manitoba province.  Eventually met his fate at the end of a rope (a few hours before I was born, modulo the year).  The province of Manitoba, last year, designated this day (not historically relevant, but a holiday throughout Canada) as Louis Riel day.

John Fredsted's picture

History

Thanks for those fascinating historical facts. The title of the song by Runrig is Rocket To The Moon.

 

Strings, yes sounds good

Hi Axel, Joe and John

thanks for the quick help. I didn't figure in the beginning that adding a plain converted i to string entry could help, but now it sounds reasonable.

Well, congratulations to MaplePrime award and have a nice evening

 

Karl

File Handles

A practical point that we overlooked is accessing these files.  The call to fopen returns a file handle (a posint) that can be used for later reference, rather than the filename.  Thus,

for i to 3 do
  fd[i] := fopen(sprintf("file%d.txt", i), 'WRITE');
end do:

Then you could do

for i to 3 do
    fprintf(fd[i], "Writing to file %d\n", i);

end do;
 
When finished,
 

fclose(seq(fd[i], i=1..3));

 

How about using cat?

Can you generate the names using cat?

 

cat("file",i,".txt");

 

 

cat, with range

That works as well.  In fact, you could do the following to create a list of the files names:

files := [cat]("file", 1..3, ".txt");   
               files := ["file1.txt", "file2.txt", "file3.txt"]

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}