Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
December 07, 2020 how to access record[0] of a csv row? Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar) | ||||
---|---|---|---|---|
| ||||
Hi, I'm trying this code: i.e. print out the 1st element of each row https://run.dlang.io/is/pG921a void main() { import std.csv; import std.stdio: write, writeln, writef, writefln; import std.algorithm.comparison : equal; string text = "76,26,22"; auto records = text.csvReader!int; foreach(r; records) {writeln(r[0]);} // line 8 assert(records.equal!equal([ [76, 26, 22], ])); } but I got a compile error: onlineapp.d(8): Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar) should `r`'s type be integer array? and how do I access each elelment of the row? Thanks. |
December 07, 2020 Re: how to access record[0] of a csv row? Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar) | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On Monday, 7 December 2020 at 02:25:23 UTC, mw wrote: > onlineapp.d(8): Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar) > > should `r`'s type be integer array? and how do I access each elelment of the row? > > Thanks. The docs [1] say that csvReader returns an input range, not an array. Input ranges don't support indexing, only iteration (e.g. with `foreach`). If you want an array, you will have to use `std.array.array` to create one. [1] http://phobos.dpldocs.info/std.csv.csvReader.1.html |
December 07, 2020 Re: how to access record[0] of a csv row? Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Monday, 7 December 2020 at 03:51:02 UTC, Paul Backus wrote: > On Monday, 7 December 2020 at 02:25:23 UTC, mw wrote: >> onlineapp.d(8): Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar) >> >> should `r`'s type be integer array? and how do I access each elelment of the row? >> >> Thanks. > > The docs [1] say that csvReader returns an input range, not an array. Input ranges don't support indexing, only iteration (e.g. with `foreach`). If you want an array, you will have to use `std.array.array` to create one. > > [1] http://phobos.dpldocs.info/std.csv.csvReader.1.html Thanks. I saw the 1st example on this page: https://dlang.org/phobos/std_csv.html foreach (record; csvReader!(Tuple!(string, string, int))(text)) { writefln("%s works as a %s and earns $%d per year", record[0], record[1], record[2]); } So my next question: given N, how do I create a Tuple!(double, double, ... n-double) type programmatically? |
December 07, 2020 Re: how to access record[0] of a csv row? Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar) | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On Monday, 7 December 2020 at 04:03:05 UTC, mw wrote:
> So my next question: given N, how do I create a Tuple!(double, double, ... n-double) type programmatically?
import std.meta: Repeat;
alias NDoubles = Tuple!(Repeat!(N, double));
Note that N must be a compile-time constant, since the number of elements in a Tuple is fixed at compile time.
|
December 07, 2020 Re: how to access record[0] of a csv row? Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Monday, 7 December 2020 at 04:38:07 UTC, Paul Backus wrote:
> On Monday, 7 December 2020 at 04:03:05 UTC, mw wrote:
>> So my next question: given N, how do I create a Tuple!(double, double, ... n-double) type programmatically?
>
> import std.meta: Repeat;
>
> alias NDoubles = Tuple!(Repeat!(N, double));
>
> Note that N must be a compile-time constant, since the number of elements in a Tuple is fixed at compile time.
Yes, I just realized that Tuple (upper T, compile time) and tuple (lower t) are different things.
Now, how to convert it to a native array:
double[] row = record;
Error: cannot implicitly convert expression record of type Tuple!(double, double, double, ..., double) to double[]
(I know for tuple, we can do: double[] arr = [record];)
|
December 07, 2020 Re: how to access record[0] of a csv row? Error: no [] operator overload for type CsvRecord!(int, cast(Malformed)1, string, dchar) | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On Monday, 7 December 2020 at 06:18:33 UTC, mw wrote:
> Now, how to convert it to a native array:
>
> double[] row = record;
>
> Error: cannot implicitly convert expression record of type Tuple!(double, double, double, ..., double) to double[]
>
> (I know for tuple, we can do: double[] arr = [record];)
double[] row = [record.expand];
|
Copyright © 1999-2021 by the D Language Foundation