Multidimensional dynamic array of strings initialized with split()
H. S. Teoh
hsteoh at quickfur.ath.cx
Wed Sep 4 16:04:41 PDT 2013
On Thu, Sep 05, 2013 at 12:57:34AM +0200, Ludovit Lucenic wrote:
> Hello friends,
>
> with the following code
>
> import std.stdio;
> import std.array;
>
> auto file71 = File(argv[2], "r");
>
> string[][] buffer;
> foreach (line; file71.byLines) {
> buffer ~= split(line, "\t");
> }
>
> I am trying to cut the lines from the file with tab as delimiter to
> pre-fetch the content of a file before further processing.
>
> Each split() call gives correct string[] values in and of itself.
> But when I try to read buffer, after the loop, I got corrupted data,
> like this:
>
> [ ["-", "_Unit226", "constructor", "sub_00BE896C\t1\t?:?\t\t//con",
> "t", "uc...
>
> Obviously the concatenation is doing no good, since there are tabs
> in the values...
>
> What am I missing here ? Is it that split() allocated memory that
> gets overwritten in the loop and the ~= just copies the subarrays
> not copying the subsubarrays ? How to overcome this ?
[...]
The problem is that File.byLine() reuses its buffer for efficiency, and
split is optimized to return slices into that buffer instead of copying
each substring. So after every iteration the buffer (and therefore the
slices into it) gets overwritten.
Replace the loop body with the following and it should work:
buffer ~= split(line.dup, "\t");
T
--
Dogs have owners ... cats have staff. -- Krista Casada
More information about the Digitalmars-d-learn
mailing list