Thread overview |
---|
January 05, 2020 Unfold string array | ||||
---|---|---|---|---|
| ||||
I couldn't find by searching this forum the way to do that, so I thought it is better to ask and learn. The data I am trying to unfold is in the form of string array read from a file line by line, like this: string[] lines = [ "key1: valueA", "key2: valueB", "key3: valueC", "\tvalueD", "<white space>valueE", "key4: valueF", ] where <white space> represents some white space character(s). It could be TAB or SPACE, etc. Now, I want to find a way to concatenate valueD to the previous element which begins with key3 and afterwards concatenate valueE also to the concatenated string for key3. The rule is that long strings are folded when written to the file and folded lines always begin with a white space. Finally I am going to use assocArray to construct an associative array. All advises are welcome Thank you! |
January 05, 2020 Re: Unfold string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Teo | On Sunday, 5 January 2020 at 08:21:54 UTC, Teo wrote: > All advises are welcome > Thank you! For some reason I can't get run.dlang.io to shorten a link... hmm... I'll use ideone. https://ideone.com/EjbhIs It's kind of a naive implementation, there probably is room for improvement but should work for start. |
January 05, 2020 Re: Unfold string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to JN | On Sunday, 5 January 2020 at 13:37:58 UTC, JN wrote:
> It's kind of a naive implementation, there probably is room for improvement but should work for start.
Thanks for the input.
I just realized that I was not precise enough in my description. Apologies for that.
My intention is to use std.algorithm, if possible.
I read the documentation and tried using many functions like "joiner", "each", "fold", "group", "filter", etc. from std.algorithm.iteration.
In my opinion, I need to construct a predicate like
(a, b) => if b.startsWith(<white space>) {join(a, b, " ")}
Unfortunately, I cannot figure out which combination of functions will give me the desired result.
Of course, if I am unable to find an elegant way to do it, I am going to iterate over the array in a way similar to what you suggested.
|
January 06, 2020 Re: Unfold string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Teo | On Sunday, 5 January 2020 at 22:39:37 UTC, Teo wrote: > On Sunday, 5 January 2020 at 13:37:58 UTC, JN wrote: >> [...] > > Thanks for the input. > I just realized that I was not precise enough in my description. Apologies for that. > My intention is to use std.algorithm, if possible. > > I read the documentation and tried using many functions like "joiner", "each", "fold", "group", "filter", etc. from std.algorithm.iteration. > > In my opinion, I need to construct a predicate like > (a, b) => if b.startsWith(<white space>) {join(a, b, " ")} > Unfortunately, I cannot figure out which combination of functions will give me the desired result. > > Of course, if I am unable to find an elegant way to do it, I am going to iterate over the array in a way similar to what you suggested. https://ideone.com/tvpreP |
January 06, 2020 Re: Unfold string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | On Monday, 6 January 2020 at 00:03:06 UTC, Temtaime wrote:
>
> https://ideone.com/tvpreP
That's cool! Thank you very much!
|
Copyright © 1999-2021 by the D Language Foundation