I am learning D, and I have a simple program that reads a text file line by line, splits each line into its own separate words, and prints it all to stdout.
import std.stdio;
import std.string;
void main(string args[])
{
char[][][] lines;
auto input = File(args[1], "r");
foreach(line; input.byLine())
{
auto words = split(strip(line));
lines ~= words;
}
foreach(line; lines)
{
writeln(line);
}
}
The code to create is created words. If I just call writelnin words every time he is assigned, I get the conclusion I want. But if I add wordsin linesand output lines, something strange will happen. lineshas an entry for each line in the source file, but each line is a corrupt version of the last line. For example, if the last line of a file looks like this:
END START * End of routine
I get an output that looks something like this:
[ , END, ST, *, End , f rout, ne, , , e other]
[ , END, ST, *, End of, rout, ne, , , e othe]
[ , END, STAR, *, End of, rout, ne.,
e]
[ , END, START , *, End of, rout, ne.,
e]
[END , STAR]
[ , END, START , *, End , f , out, ne. ]
[END, START, *, End, of ro, tine. , , ,
]
[END, STA, *, o, r, ut]
[ , END , S, *, End, o, r, utine., , , ,
, o]
[END, START , *, of routi, e., ]
Any idea what I'm doing wrong?