digitalmars.D.learn - how to access record[0] of a csv row? Error: no [] operator overload
- mw (21/21) Dec 06 2020 Hi,
- Paul Backus (6/11) Dec 06 2020 The docs [1] say that csvReader returns an input range, not an
- mw (11/24) Dec 06 2020 Thanks.
- Paul Backus (5/7) Dec 06 2020 import std.meta: Repeat;
- mw (8/15) Dec 06 2020 Yes, I just realized that Tuple (upper T, compile time) and tuple
- Paul Backus (2/7) Dec 07 2020 double[] row = [record.expand];
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.
Dec 06 2020
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
Dec 06 2020
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: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?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
Dec 06 2020
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.
Dec 06 2020
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: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];)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.
Dec 06 2020
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];
Dec 07 2020