reading file byLine
Namal via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Sep 18 04:11:49 PDT 2015
On Friday, 18 September 2015 at 11:06:46 UTC, Edwin van Leeuwen
wrote:
> On Friday, 18 September 2015 at 10:48:25 UTC, Namal wrote:
>> On Friday, 18 September 2015 at 10:34:41 UTC, Edwin van
>> Leeuwen wrote:
>>> On Friday, 18 September 2015 at 10:26:46 UTC, Namal wrote:
>>>> Hello guys, is there a nice functional way to read the file
>>>> which is like
>>>>
>>>>
>>>> 1,2,3,4,5,6
>>>> 2,3,4,5,6,7
>>>> 8,9,0,9,2,3
>>>>
>>>> line by line, split numbers and remove each ','
>>>> convert it to int and save in a matrix int[][] arr?
>>>
>>> Not tested, but I think the following should work:
>>>
>>> auto matrix = str
>>> .byLine
>>> .map!((l) => l.split(",") // Split each line
>>> .map!(to!int) // Turn into ints
>>> .array) // Return an array
>>> .array // Copy into an array
>>
>> And how do tell here to read my file?
>
> Replace str with File("myfile"):
>
> auto matrix = File("myfile")
> .byLine
> .map!((l) => l.split(",") // Split each line
> .map!(to!int) // Turn into ints
> .array) // Return an array
> .array // Copy into an array
import std.file, std.stdio, std.string, std.conv, std.algorithm,
std.array;
void main(){
auto matrix = File("test.txt")
.byLine
.map!((l) => l.split(",") // Split each line
.map!(to!int) // Turn into ints
.array) // Return an array
.array();
matrix.writeln;
}
compiles but crashes
More information about the Digitalmars-d-learn
mailing list