csvReader: how to read only selected columns while the class Layout has extra field?
Salih Dincer
salihdb at hotmail.com
Mon Oct 3 18:02:51 UTC 2022
On Sunday, 2 October 2022 at 19:48:52 UTC, mw wrote:
> ```
> text.csvReader!Layout(["b","c","a"]); // Read only
> these column
> ```
>
> The intention is very clear: only read the selected columns
> from the csv, and for any other fields of class Layout, just
> ignore (with the default D .init value).
Why don't you do this? For example you can try the following?
```d
import std.csv, std.math.algebraic : abs;
string str = "a,b,c\nHello,65,63.63\n➊➋➂❹,123,3673.562";
struct Layout
{
int value;
double other;
string name;
}
auto records = csvReader!Layout(str, ["b","c","a"]);
Layout[2] ans;
ans[0].name = "Hello";
ans[0].value = 65;
ans[0].other = 63.63;
ans[1].name = "➊➋➂❹";
ans[1].value = 123;
ans[1].other = 3673.562;
int count;
foreach (record; records)
{
assert(ans[count].name == record.name);
assert(ans[count].value == record.value);
assert(abs(ans[count].other - record.other) < 0.00001);
count++;
}
assert(count == ans.length);
```
SDB at 79
More information about the Digitalmars-d-learn
mailing list