reading from file
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Dec 15 23:03:40 PST 2016
On 12/15/2016 10:47 PM, KaattuPoochi wrote:
> On Tuesday, 13 December 2016 at 21:13:26 UTC, Ali wrote:
>>
>> And extending Ali's solution you can actually get the data in
>> to a two dimentional array at compile time and have it in static
>> memory with a small adjustment:
>>
>> static immutable matrix = import("data.txt")
>> .split("\n")
>> .map!(a => a.split(",").map!(to!int).array)
>> .array;
>>
>> void main() {
>> writeln(matrix);
>> }
>
> 1. For any non-trivial matrices (with 500 lines) runs DMD 2.072.1 out of
> memory (2GB). Not sure if this is a known bug. Works fine with LDC 1.0.0.
Compile time features are awesome but currenty very inefficient. :)
> 2. The EOL on the last line results in an empty row in the end. Is there
> a way to overcome this?
If appropriate, you can filter out all empty lines. Added .map!strip and
.filter:
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.string;
void main() {
auto a = File("deneme.txt")
.byLine
.map!strip
.filter!(line => !line.empty)
.map!(line => line.splitter(',').map!(to!int).array)
.array;
writeln(a);
}
Ali
More information about the Digitalmars-d-learn
mailing list