csvReader: how to read only selected columns while the class Layout has extra field?

rassoc rassoc at posteo.de
Sun Oct 2 21:03:40 UTC 2022


On 10/2/22 21:48, mw via Digitalmars-d-learn 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).
> 

Here's why it's not currently working:

"An optional header can be provided. The first record will be read in as the header. If Contents is a struct then the header provided is expected to correspond to the fields in the struct."

"expected to correspond" means that the number of fields in the content struct can't exceed the header element count as you can see in the actual code [1]:

```
foreach (ti, ToType; Fields!(Contents))
{
     if (indices[ti] == colIndex) // indices.length depends on passed in colHeaders.length
     ...
}
```

The current index exception is bad, this needs an assert in the constructor with a nicer error message.

But say, I'm curious, what's the purpose of adding an optional/useless contents field? What's the use-case here?

[1] https://github.com/dlang/phobos/blob/8e8aaae5080ccc2e0a2202cbe9778dca96496a95/std/csv.d#L1209


More information about the Digitalmars-d-learn mailing list