Efficiently streaming data to associative array
Jon Degenhardt via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Aug 10 05:33:46 PDT 2017
On Wednesday, 9 August 2017 at 13:36:46 UTC, Steven Schveighoffer
wrote:
> On 8/8/17 3:43 PM, Anonymouse wrote:
>> On Tuesday, 8 August 2017 at 16:00:17 UTC, Steven
>> Schveighoffer wrote:
>>> I wouldn't use formattedRead, as I think this is going to
>>> allocate temporaries for a and b.
>>
>> What would you suggest to use in its stead? My use-case is
>> similar to the OP's in that I have a string of tokens that I
>> want split into variables.
>
> using splitter(","), and then parsing each field using
> appropriate function (e.g. to!)
>
> For example, the OP's code, I would do:
>
> auto r = line.splitter(",");
> a = r.front;
> r.popFront;
> b = r.front;
> r.popFront;
> c = r.front.to!int;
>
> It would be nice if formattedRead didn't use appender, and
> instead sliced, but I'm not sure it can be fixed.
>
> Note, one could make a template that does this automatically in
> one line.
>
> -Steve
The blog post Steve referred to has examples of this type
processing while iterating over lines in a file. A couple
different ways to access the elements are shown. AA access is
addressed also:
https://dlang.org/blog/2017/05/24/faster-command-line-tools-in-d/
--Jon
More information about the Digitalmars-d-learn
mailing list