www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to access record[0] of a csv row? Error: no [] operator overload

reply mw <mingwu gmail.com> writes:
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
parent reply Paul Backus <snarwin gmail.com> writes:
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
parent reply mw <mingwu gmail.com> writes:
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?
Dec 06 2020
parent reply Paul Backus <snarwin gmail.com> writes:
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
parent reply mw <mingwu gmail.com> writes:
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];)
Dec 06 2020
parent Paul Backus <snarwin gmail.com> writes:
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