Generating variable names in a loop
in
Programming Questions
•
2 years ago
I need to parse data into multiple files based on the leading digits of a value, and naming the files on the leading digits. This is just the output declaration, but I go through this each step of the way. I have to create these files and write data directly to them as I am processing ~90 GB of data each time, so arrays are not going to work.
Currently the code looks like this:
output0 = createWriter(outPath + "0.txt");
output1 = createWriter(outPath + "1.txt");
output2 = createWriter(outPath + "2.txt");
output3 = createWriter(outPath + "3.txt");
output4 = createWriter(outPath + "4.txt");
output5 = createWriter(outPath + "5.txt");
output6 = createWriter(outPath + "6.txt");
output7 = createWriter(outPath + "7.txt");
output8 = createWriter(outPath + "8.txt");
output9 = createWriter(outPath + "9.txt");
I do this for the declaration, then the creatWriter, println, flush and close, so there are literally hundreds of lines of code that could be condensed via a loop.
I'd like to do this, but haven't gotten it to successfully create the output files, then write to them, etc.:
for (int i = 0; i < 10; i++) {
output(i) = createWriter(outPath + i + ".txt");
}
Any help appreciated!
Currently the code looks like this:
output0 = createWriter(outPath + "0.txt");
output1 = createWriter(outPath + "1.txt");
output2 = createWriter(outPath + "2.txt");
output3 = createWriter(outPath + "3.txt");
output4 = createWriter(outPath + "4.txt");
output5 = createWriter(outPath + "5.txt");
output6 = createWriter(outPath + "6.txt");
output7 = createWriter(outPath + "7.txt");
output8 = createWriter(outPath + "8.txt");
output9 = createWriter(outPath + "9.txt");
I do this for the declaration, then the creatWriter, println, flush and close, so there are literally hundreds of lines of code that could be condensed via a loop.
I'd like to do this, but haven't gotten it to successfully create the output files, then write to them, etc.:
for (int i = 0; i < 10; i++) {
output(i) = createWriter(outPath + i + ".txt");
}
Any help appreciated!
1