Syntax for an Array of StringArrays


#1

hopefully a simple question:  What's the syntax for making, (and then retrieving from) an Array of StringArrays?

for example, i have the following StringArrays:
 


const StringArray alphabetFruit = {"apple", "banana", "cherry" ...}; 
const StringArray alphabetAnimals = {"ape", "bear", "cow" ...}; 
const StringArray alphabetCountries = {"Algeria", "Botswana", "Columbia" ...};

How can i put them all in a parent array (or probably OwnedArray) called 'alphabets' ??


#2

I think it's roughly the syntax:


const StringArray alphabetFruit = {"apple", "banana", "cherry" ...};

const StringArray alphabetAnimals = {"ape", "bear", "cow" ...};

const StringArray alphabetCountries = {"Algeria", "Botswana", "Columbia" ...}
 

Array<const StringArray> alphabets;

alphabets.insert(0, alphabetFruit);

alphabets.insert(1,alphabetAnimals);

etc..

Apologies if this is misleading, beginner myself!


#3

Thanks, yeah that's basically what i did....i used 'add', but it seems like it might have worked the same. 

so...umm,,,

 

how to read???


#4

Thanks to C++11 you can just write stuff like this:

Array<StringArray> foo { StringArray { "abc", "def" },
                         StringArray { "abdfdfg", "sdfsgdsf", "aasdfsd" } };

#5

is getting "sdfsdsf" as simple as foo[1][1] ?


#6

That'd be very inefficient, but would work. Most efficient would be foo.getReference (0)[0]


#7

it's a rare call to update the GUI when a selected instrument is changed.  I think i'll go with simplicity and readable code, over efficiency in this case. ;)

cheers


#8

It's not only efficency, the Array operator [] returns a copy of the element. So the complete StringArray would be copied, just to access one element.

If you use now the operator to manipulate an element, you even update the copy, which is discarded in the next line of code anyway.

E.g.

foo[0].add ("Another"); // will leave foo unchanged

foo.getReference(0).add("Another");  // will do what you expect.

I seeked for the problem a whole day, so I hope I could save you that time

Have fun...