csvReader & specifying separator problems...

Mike Parker aldacron at gmail.com
Thu Nov 14 13:08:10 UTC 2019


On Thursday, 14 November 2019 at 12:25:30 UTC, Robert M. Münch 
wrote:

> From the docs, which I find extremly hard to understand:
>
> auto csvReader(Contents = string, Malformed ErrorLevel = 
> Malformed.throwException, Range, Separator = char)(Range input, 
> Separator delimiter = ',', Separator quote = '"')


Contents, ErrorLevel, Range, and Separator are template (i.e. 
compile-time) parameters. Input, delimiter, and quote are 
function (i.e. runtime) parameters.

>
> So, let's see if I can decyphre this, step-by-step by trying 
> out:
>
> 	csv_records = csv_data.csvReader();


Here, you aren't providing any template parameters and only the 
first function parameter, so it's the equivalent to calling the 
function like so:

csvReader!(string, Malformed.throwException, typeof(csv_data), 
char)(csv_data, ',', '"');


> Which indicates some problem because not all fields are set in 
> my CSV data. So let's ignore any error by specifying 
> Malformed.ignore;
>
> 	csv_records = csv_data.csvReader(Malformed.ignore);


csv_records = csv_data.csvReader!(string, Malformed.ignore)();



>
> The docs state Malformed as 2nd parameter, since I use UFCS I 
> assume that this becomes the first parameter. I don't


Malformed is the 2nd template parameter, your UFCS value is the 
first function parameter.

> understand what the 3rd parameter (Range) is about.

Range is the type of the first parameter. It's common outside of 
Phobos use T and U for template types, but any valid symbol name 
can be used. This template has three type parameters which are 
named according to their purpose (Contents, Range, and 
Separator). Since Range is also the type of the first function 
parameter, the compiler will infer the type if you don't specify 
it.


> 4th parameter is my separator, which I need to set to ';' 
> somehow.

The fourth _template_ parameter is the _type_ of your separator 
(and is set to default to char) not the actual separator. You 
want to set the delimiter, which is the second _function_ 
parameter.

csv_records = csv_data.csvReader!(string, Malformed.ignore)(';');



More information about the Digitalmars-d-learn mailing list