Formatted input from text file

Timon Gehr timon.gehr at gmx.ch
Mon Jun 18 11:40:42 PDT 2012


On 06/18/2012 05:11 PM, IK wrote:
> Hmm does your code generate a 1D `array` as a grid, bearophile?
> Anyway thanks, I'll compile it shortly.
>
> My own code evolved to what's below and gives a Range violation.
> Also I don't understand why formattedRead can't just accept a casted
> `opSlice.dup`, is it a bug?

Yes.

>
> void main()
> {
> 	uint[20][20] grid;
> 	auto input_file = new MmFile("p011_input.txt");
> 	string temp = cast(string)input_file.opSlice;
>
> 	formattedRead(temp, "%(%(%s %)\n%)",&grid);
> }

IIRC formattedRead would require input similar to

"[[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]"

This works:

void main()
{
     auto input_file = new MmFile("p011_input.txt");
     auto text = cast(string)input_file[];
     auto grid = text.splitLines.map!(_=>_.split.to!(uint[])).array;
}


More information about the Digitalmars-d-learn mailing list