I have several hundreds of output files and I have their individual names in a string. Using the for loop I want to give a different name to the output of each loop. I modified the code given in other question I little bit, but the only ouput I am getting is names as "names[vfor($1)]", instead of different names in names string.
Original Code in which output names are given specifically.
//Clear out files from previous run output file=nestloop1.out reset; output file=nestloop2.out reset; for i(1, 10, 1); output file=nestloop1.out on; print "loop 1 iteration " i; for j(10, 100, 10); output file=nestloop2.out on; print "loop 2 iteration " j; endfor; endfor; output off;
So my question is: Is it possible to also loop the output file names such as the following?
//Clear out files from previous run names = "nestloop1.out" $| "nestloop2.out"; for i(1, rows(names), 1); output file = names[i] reset; endfor; for k(1, rows(names), 1); for i(1, 10, 1); output file= names[k] on; print "loop 1 iteration " i; for j(10, 100, 10); output file= names[k] on; print "loop 2 iteration " j; endfor; endfor; endfor; output off;
2 Answers
0
You can do what you want by assigning a temporary variable to hold the current file name and then using the caret operator (^) to tell GAUSS to use the value of the string, rather than the name of the string. For example:
Use name of string
//write data to a file 'mydata.txt' output file = mydata.txt reset; print "this message should appear in mydatata.txt"; output off;
Use value of string
//write data to a file 'mydata.txt' filename = "mydata.txt"; output file = ^filename reset; print "this message should appear in mydatata.txt"; output off;
Adapted to your initial code snippet
//Clear out files from previous run names = "nestloop1.out" $| "nestloop2.out"; for i(1, rows(names), 1); //make temporary variable to hold filename filename = names[i]; //Use caret operator (^) to tell GAUSS to use value of string output file = ^filename reset; endfor; for k(1, rows(names), 1); for i(1, 10, 1); //make temporary variable to hold filename filename = names[k]; //Use caret operator (^) to tell GAUSS to use value of string output file= ^filename on; print "loop 1 iteration " i; for j(10, 100, 10); //make temporary variable to hold filename //(should the index be 'k' or 'j'?) filename = names[k]; //Use caret operator (^) to tell GAUSS to use value of string output file= ^filename on; print "loop 2 iteration " j; endfor; endfor; endfor; output off;
0
Thank you so much, it worked perfectly.
Your Answer
2 Answers
You can do what you want by assigning a temporary variable to hold the current file name and then using the caret operator (^) to tell GAUSS to use the value of the string, rather than the name of the string. For example:
Use name of string
//write data to a file 'mydata.txt' output file = mydata.txt reset; print "this message should appear in mydatata.txt"; output off;
Use value of string
//write data to a file 'mydata.txt' filename = "mydata.txt"; output file = ^filename reset; print "this message should appear in mydatata.txt"; output off;
Adapted to your initial code snippet
//Clear out files from previous run names = "nestloop1.out" $| "nestloop2.out"; for i(1, rows(names), 1); //make temporary variable to hold filename filename = names[i]; //Use caret operator (^) to tell GAUSS to use value of string output file = ^filename reset; endfor; for k(1, rows(names), 1); for i(1, 10, 1); //make temporary variable to hold filename filename = names[k]; //Use caret operator (^) to tell GAUSS to use value of string output file= ^filename on; print "loop 1 iteration " i; for j(10, 100, 10); //make temporary variable to hold filename //(should the index be 'k' or 'j'?) filename = names[k]; //Use caret operator (^) to tell GAUSS to use value of string output file= ^filename on; print "loop 2 iteration " j; endfor; endfor; endfor; output off;
Thank you so much, it worked perfectly.