Schroedinger's Ranges

Paul Backus snarwin at gmail.com
Thu Jun 3 01:22:14 UTC 2021


On Thursday, 3 June 2021 at 00:39:04 UTC, vacuum_tube wrote:
> I've been trying to make a struct for CSV parsing and 
> manipulating.  The code was as follows:
> ```
> struct CSVData(bool HeaderFromFirstLine)
> {
> 	char[][] header = [];
> 	char[][][] rest = [];
>
> 	this(string filename)
> 	{
> 		auto tmp = File(filename).byLine();
> 		
> 		if(HeaderFromFirstLine)
> 		{
> 			this.header = CSVData.parseCSV(tmp.front()).array;
> 			tmp.popFront();
> 		}
>
> 		this.rest = tmp.map!(e => parseCSV(e)).array;
> 	}
> ```
[...]
> The "testdata" text file looked like this:
> ```
> 10,15,Hello world
> stuff,,more stuff
> ```
> And the output from running it looked like this:
> ```
> ["st", "ff", ",more stuff"]
> ["stuff", "", "more stuff"]

`File.byLine` overwrites the previous line's data every time it 
reads a new line. If you want to store each line's data for later 
use, you need to use [`byLineCopy`][1] instead.

[1]: https://phobos.dpldocs.info/std.stdio.File.byLineCopy.1.html


More information about the Digitalmars-d-learn mailing list