Multidimensional dynamic array of strings initialized with split()

Ali Çehreli acehreli at yahoo.com
Thu Sep 5 09:22:43 PDT 2013


On 09/05/2013 01:14 AM, Ludovit Lucenic wrote:
> I have created a wiki on this one.
> http://wiki.dlang.org/Read_table_data_from_file
>

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

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



More information about the Digitalmars-d-learn mailing list