Thread overview | |||||
---|---|---|---|---|---|
|
May 08, 2015 Chaining input | ||||
---|---|---|---|---|
| ||||
I have the following code that converts input like blah, blub, gobble, dygook to string[] auto f = File("file.txt", "r"); auto words = f.byLine .map!( a => a.to!(string) .splitter(", ") .filter!(a => a.length) ) .copy(appender!(string[])).data; I'm sure there is room for improvement. D is pretty cool in this way. Once you get used to this kind of code, you're spoiled forever. |
May 08, 2015 Re: Chaining input | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | On Friday, 8 May 2015 at 11:00:01 UTC, Chris wrote: > > I'm sure there is room for improvement. > It looks like your reading some kind of comma seperated values (csv). have a look at std.csv of phobos ``` foreach(record; file.byLine.joiner("\n").csvReader!(Tuple!(string, string, int))) { writefln("%s works as a %s and earns $%d per year", record[0], record[1], record[2]); } ``` |
May 08, 2015 Re: Chaining input | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert burner Schadek | On Friday, 8 May 2015 at 11:14:43 UTC, Robert burner Schadek wrote:
> On Friday, 8 May 2015 at 11:00:01 UTC, Chris wrote:
>>
>> I'm sure there is room for improvement.
>>
>
> It looks like your reading some kind of comma seperated values (csv).
>
> have a look at std.csv of phobos
>
> ```
> foreach(record;
> file.byLine.joiner("\n").csvReader!(Tuple!(string, string, int)))
> {
> writefln("%s works as a %s and earns $%d per year",
> record[0], record[1], record[2]);
> }
> ```
Yeah, I actually wanted to have a closer look at std.csv. But in this case the input format may change (comments, keywords etc.), so I might need a customized parser anyway.
|
Copyright © 1999-2021 by the D Language Foundation