Thread overview | |||||
---|---|---|---|---|---|
|
June 07, 2014 How to add elements to dynamic array of dynamic array | ||||
---|---|---|---|---|
| ||||
I am very new to D. How do I add elements to a dynamic array of dynamic array. This is my code that fails to compile. void ex1() { alias the_row = string[]; alias the_table = the_row[]; File inFile = File("account.txt", "r"); while (!inFile.eof()) { string row_in = chomp(inFile.readln()); string[] row_out = split(row_in,"\t"); the_table ~= row_out; //Error: string[][] is not an lvalue } writeln(the_table.length); // Error: string[][] is not an expression } |
June 07, 2014 Re: How to add elements to dynamic array of dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to katuday | katuday: > I am very new to D. Welcome to D :-) > alias the_row = string[]; > alias the_table = the_row[]; Here you are defining two types (and in D idiomatically types are written in CamelCase, so TheRow and TheTable). > File inFile = File("account.txt", "r"); This is enough: auto inFile = File("account.txt", "r"); > while (!inFile.eof()) > { > string row_in = chomp(inFile.readln()); This should be better (untested): foreach (rawLine; inFile.byLine) { auto row = rawLine.chomp.idup.split("\t"); > the_table ~= row_out; //Error: string[][] is not an lvalue theTable is not a variable, it's a type, so you need to define it like this: string[][] table; Bye, bearophile |
June 07, 2014 Re: How to add elements to dynamic array of dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Saturday, 7 June 2014 at 17:04:36 UTC, bearophile wrote:
> katuday:
>
>> I am very new to D.
>
> Welcome to D :-)
>
>
>> alias the_row = string[];
>> alias the_table = the_row[];
>
> Here you are defining two types (and in D idiomatically types are written in CamelCase, so TheRow and TheTable).
>
>
>> File inFile = File("account.txt", "r");
>
> This is enough:
>
> auto inFile = File("account.txt", "r");
>
>
>> while (!inFile.eof())
>> {
>> string row_in = chomp(inFile.readln());
>
> This should be better (untested):
>
> foreach (rawLine; inFile.byLine) {
> auto row = rawLine.chomp.idup.split("\t");
>
>
>> the_table ~= row_out; //Error: string[][] is not an lvalue
>
> theTable is not a variable, it's a type, so you need to define it like this:
>
> string[][] table;
>
> Bye,
> bearophile
Thank you.
|
Copyright © 1999-2021 by the D Language Foundation