Read table data from file

From D Wiki
Jump to: navigation, search

Reading tabular data from file

To read in a text file with records in rows, where fields are separated by a separator (e.g. tab, whitespace), this code might help:

 1 module test.read;
 2 
 3 import std.stdio;
 4 import std.array;
 5 
 6 auto readInData(File inputFile, string fieldSeparator)
 7 {
 8     string[][] buffer;
 9 
10     foreach (line; inputFile.byLine)
11         buffer ~= split(line.idup, fieldSeparator);
12 
13     return buffer;
14 }
15 
16 void main(string[] args)
17 {
18     if (args.length > 1)
19         writeln(readInData(
20             File(args[1]),
21             args.length > 2 ? args[2] : " "
22         ));
23 }


The not so obvious usage of .idup property is necessary here in order to avoid memory rewrite of the output multidimensional string array. There are couple of reasons for this:

  • the LineReader defined with File.byLine() reuses its buffer for efficiency and
  • split() function is optimized to return slices into its input buffer (line in this case) instead of copying each substring to output.

Without the line being duplicated the output buffer gets overwritten in every iteration.

Credits

The information provided here was first discussed on this thread.