September 18, 2015 Re: reading file byLine | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | 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
|
September 18, 2015 Re: reading file byLine | ||||
---|---|---|---|---|
| ||||
Posted in reply to Edwin van Leeuwen | 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
|
September 18, 2015 Re: reading file byLine | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Friday, 18 September 2015 at 11:11:51 UTC, Namal wrote:
> compiles but crashes
For me it works fine. You probably have extra spaces or something in your file. It would help if you posted the error message you get when running the program.
|
September 18, 2015 Re: reading file byLine | ||||
---|---|---|---|---|
| ||||
Posted in reply to Edwin van Leeuwen | On Friday, 18 September 2015 at 11:37:15 UTC, Edwin van Leeuwen wrote:
> On Friday, 18 September 2015 at 11:11:51 UTC, Namal wrote:
>> compiles but crashes
>
> For me it works fine. You probably have extra spaces or something in your file. It would help if you posted the error message you get when running the program.
Oh, yes, sorry, there was a space after the end of one line
|
September 18, 2015 Re: reading file byLine | ||||
---|---|---|---|---|
| ||||
Posted in reply to Edwin van Leeuwen | So do I understand it right: does => in map! indicates a lambda function? |
September 18, 2015 Re: reading file byLine | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Friday, 18 September 2015 at 12:28:29 UTC, Namal wrote: > So do I understand it right: does => in map! indicates a lambda function? Yes exactly. There are a number of ways you can define a lambda function in D. For example if the function is multiline I often use: (l) { ...; // do something return result; } More details here http://ddili.org/ders/d.en/lambda.html (half way down the page) |
September 18, 2015 Re: reading file byLine | ||||
---|---|---|---|---|
| ||||
Posted in reply to Edwin van Leeuwen | On 09/18/2015 05:58 AM, Edwin van Leeuwen wrote: > On Friday, 18 September 2015 at 12:28:29 UTC, Namal wrote: >> So do I understand it right: does => in map! indicates a lambda >> function? > > Yes exactly. There are a number of ways you can define a lambda function > in D. For example if the function is multiline I often use: > (l) { > ...; // do something > return result; > } > > More details here http://ddili.org/ders/d.en/lambda.html (half way down > the page) As of a few hours ago, you can go to the book index and search for =>, or lambda, or anything else. :) http://ddili.org/ders/d.en/ix.html (Excuse the page format for now.) Ali |
November 07, 2015 Re: reading file byLine | ||||
---|---|---|---|---|
| ||||
Posted in reply to deed | On Saturday, 5 September 2015 at 14:49:13 UTC, deed wrote:
> On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote:
>> .map!(s => chomp(s, "\"")
>> .map!(s => chompPrefix(s, "\"")
>
> should be
>
> .map!(s => chomp(s, "\""))
> .map!(s => chompPrefix(s, "\""))
Hello again,
Now I have a file that looks like
a b c d
e f g h
....
I want to get that in an element of strings but without quatation marks
auto a = f.byLine()
.map!(a => a.split)
.array();
f.close();
|
November 07, 2015 Re: reading file byLine | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Saturday, 7 November 2015 at 17:13:33 UTC, Namal wrote:
> On Saturday, 5 September 2015 at 14:49:13 UTC, deed wrote:
>> On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote:
>>> .map!(s => chomp(s, "\"")
>>> .map!(s => chompPrefix(s, "\"")
>>
>> should be
>>
>> .map!(s => chomp(s, "\""))
>> .map!(s => chompPrefix(s, "\""))
>
> Hello again,
>
> Now I have a file that looks like
>
> a b c d
> e f g h
> ....
>
> I want to get that in an element of strings but without quatation marks
>
> auto a = f.byLine()
> .map!(a => a.split)
> .array();
>
> f.close();
Sorry for double post, I pressed accidently 'enter'. How can I ad chomp so the quatation marks are removed?
|
Copyright © 1999-2021 by the D Language Foundation