Multidimensional dynamic array of strings initialized with split()

Ludovit Lucenic llucenic at gmail.com
Fri Sep 22 17:50:17 UTC 2017


On Thursday, 5 September 2013 at 16:22:46 UTC, Ali Çehreli wrote:
>
> Compiling with "DMD64 D Compiler v2.064-devel-52cc287" produces 
> the following errors:
>
> * You had byLines in your original code as well. Shouldn't it 
> be byLine?
>
> * You are missing the closing brace of the foreach loop as well.
>
> * "Error: cannot append type char[][] to type string[][]" I 
> have to replace .dup with .idup

Thank you for pointing out the errors, Ali.
I have updated the example.

>
> The following version is lazy:
>
> import std.stdio;
> import std.array;
> import std.algorithm;
>
> auto readInData(File inputFile, string fieldSeparator)
> {
>     return
>         inputFile
>         .byLine
>         .map!(line => line
>                       .idup
>                       .split("\t"));
> }
>
> The caller can either use the result lazily:
>
> import std.range;
>
> void main()
> {
>     auto file = File("deneme.txt");
>     writeln(readInData(file, "\t").take(2));
> }
>
> Or call .array on the result to consume the range eagerly:
>
>     auto table = readInData(file, "\t").array;
>
> Ali

Thank you for the alternative approaches. This thread is linked 
from Credits section, if someone wants to find out more on the 
topic from the wiki.


More information about the Digitalmars-d-learn mailing list