Thread overview |
---|
February 24, 2018 How to split a string to a 2D array | ||||
---|---|---|---|---|
| ||||
I want to convert a string like " a,b<br/>1, 2<br/>3<br/> " to a 2D array like: [["a", "b"], ["1", "2"], ["3", "" ]] auto html = " a,b<br/>1, 2<br/>3<br/> "; auto rows = html.strip.chomp("<br/>").split("<br/>"); string[][] data; rows.each!(a => data ~= a.split(",")); string[][] result = data.map!(a => a.padRight("", data[0].length).map!(b => b.strip)).array; but the result is not a string[][]: Error: cannot implicitly convert expression `array(map(data))` of type `MapResult!(__lambda2, Result)[]` to `string[][]` |
February 24, 2018 Re: How to split a string to a 2D array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Domain | On Saturday, 24 February 2018 at 07:51:27 UTC, Domain wrote: > I want to convert a string like " a,b<br/>1, 2<br/>3<br/> " to a 2D array like: > [["a", "b"], > ["1", "2"], > ["3", "" ]] > > > auto html = " a,b<br/>1, 2<br/>3<br/> "; > auto rows = html.strip.chomp("<br/>").split("<br/>"); > string[][] data; > rows.each!(a => data ~= a.split(",")); > string[][] result = data.map!(a => a.padRight("", data[0].length).map!(b => b.strip)).array; > > but the result is not a string[][]: > > Error: cannot implicitly convert expression `array(map(data))` of type `MapResult!(__lambda2, Result)[]` to `string[][]` And why this not compile: rows.each!(a => data ~= a.split(",").map!(b => b.strip).padRight("", 2)); Error: cannot deduce function from argument types !()(string[]), candidates are: C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(899): C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(934): |
February 24, 2018 Re: How to split a string to a 2D array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Domain | On Saturday, 24 February 2018 at 08:59:46 UTC, Domain wrote:
> On Saturday, 24 February 2018 at 07:51:27 UTC, Domain wrote:
>> [...]
>
> And why this not compile:
>
> rows.each!(a => data ~= a.split(",").map!(b => b.strip).padRight("", 2));
>
>
> Error: cannot deduce function from argument types !()(string[]), candidates are:
> C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(899): C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(934):
OK, this compile:
rows.each!(a => data ~= a.split(",").map!(b => b.strip).padRight("", 2).array);
|
February 24, 2018 Re: How to split a string to a 2D array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Domain | On Saturday, 24 February 2018 at 09:06:09 UTC, Domain wrote: > On Saturday, 24 February 2018 at 08:59:46 UTC, Domain wrote: >> On Saturday, 24 February 2018 at 07:51:27 UTC, Domain wrote: >>> [...] >> >> And why this not compile: >> >> rows.each!(a => data ~= a.split(",").map!(b => b.strip).padRight("", 2)); >> >> >> Error: cannot deduce function from argument types !()(string[]), candidates are: >> C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(899): C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(934): > > OK, this compile: > rows.each!(a => data ~= a.split(",").map!(b => b.strip).padRight("", 2).array); FYI: Idiomatic D would be to avoid allocations (faster!). An example: ``` auto html = " a,b<br/>1, 2<br/>3<br/> "; auto rows = html.strip.splitter("<br/>").filter!(not!empty).map!(a => a.splitter(",")); rows.writeln; ``` https://run.dlang.io/is/LC7Sog If you really, really need a 2d array you can always do so if required at the end: ``` auto arr2d = rows.map!array.array; ``` But please keep in mind that you ideally avoid the allocations in the first place. |
Copyright © 1999-2021 by the D Language Foundation