reading file byLine

Edwin van Leeuwen via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 18 04:06:44 PDT 2015


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



More information about the Digitalmars-d-learn mailing list