get values from an arrayname inside an array ?

Hi all,

did not find any helpful info for my issue so far…

For my model railway station I would like to create a Track-Display using a NANO and a small OLED Display that shows the next Trainnumber, Target Station, Track Number and time until departure.
Basically that works, but I would like to do this with an array as follows:

const String Arr_Salzburg[6] {“SALZBURG;RJ65;;4;9;”, “SALZBURG;RJ65;;4;7;”, “SALZBURG;RJ65;;4;4;”, “SALZBURG;RJ65;;4;2;”, “SALZBURG;RJ65;;4;1;”, “SALZBURG;RJ65;;4;0;”};
So in a for-loop I would like to iterate through the array and update time until departure - depending on the amount of entries in the array.

to be more flexible I would like to have another array that contains all arrays with track info e.g.
const String Arraylist[10] = {“Arr_Salzburg”};

My issue is - in the first for loop thru Arraylist I get Arr_Salzburg back but I am not able to use value Arr_Salzburg for another for loop to get its values…

I was not able to use Arraylist in the second for- loop.
=> how can I get the content of Array Arraylist ?
```
*  for (byte h = 0; h < (sizeof(Arraylist)) / sizeof(Arraylist[0]); h++) {
    Serial.println(Arraylist[h]); //debug

for (byte i = 0; i < (sizeof(Arraylist[h]) / sizeof(Arraylist[h][0])); i++) {

Serial.println(Arraylist[h][i]);  //debug

str1stline = split(Arraylist[h][i], ‘;’, 0);
_
```*
many thanks for any help,
Wolfgang_

The name of an array in a String won't do you any good. Once the sketch is compiled, the names are gone and everything is done by address.

const String  Arr_Salzburg[6] {
    "SALZBURG;RJ65;;4;9;", 
    "SALZBURG;RJ65;;4;7;", 
    "SALZBURG;RJ65;;4;4;", 
    "SALZBURG;RJ65;;4;2;", 
    "SALZBURG;RJ65;;4;1;", 
    "SALZBURG;RJ65;;4;0;"};

const String  *Arraylist[] = {Arr_Salzburg};

Arraylist[0][2] points to the third element of Arr_Salzburg which is the String "SALZBURG;RJ65;;4;4;"

If you want to use {"SALZBURG", "RJ65", 4, 4} as data you should NOT put them in a String where you have to parse them every time you want the values. A 'struct' is a way to put several data items together into one array element.

struct Route {const char *stationName, const char *routeName, unsigned travelTime, unsigned arrivalTime};

Hi John,
THX for your reply!

The data within the string {"SALZBURG", "RJ65", 4, 4} is used as string only - so no deed to convert the types.

What I wanted to archive is all information for one display sequence in one array that can be processed in one loop (independent how many
elements this array may have)
e.g. the Salzburg- Array with all info for Track 4 until the train departed.
Then I planned to have a seperate Linz, Vienna, Airport, etc etc array for more trains.

to get more flexibility into that I wanted to put all Array names into another array - Arraylist - and process this

  • in a loop from first to last
  • randomly
    perhaps * selectable

Using a struct was one idea but if I understood that correct, it kindof replaces my first String Array but does not solve my actual issue to process the
elements of Array "struct Route" inside Array Arraylist....

=> I think I would need an array "struct Route" (in my Arr_Salzburg[6]) and this in an additional array Arraylist.

regards, Wolfgang

Huuuuu I think now I got your suggestion....

I can use a struct in a 2 dimensional array.

ArrStationlist.[targetstation].[asc_number]

means (simplified)

ArrStationlist.[1].[1] = "SALZBURG;RJ65;;4;9;"
ArrStationlist.[1].[2] = "SALZBURG;RJ65;;4;4;"
ArrStationlist.[1].[3] = "SALZBURG;RJ65;;4;0;"
ArrStationlist.[2].[1] = "VIENNA;RJ65;;2;3;"
ArrStationlist.[2].[2] = "VIENNA;RJ65;;2;0;"
ArrStationlist.[3].[1] = "GRAZ;RJ65;;1;9;"
ArrStationlist.[3].[2] = "GRAZ;RJ65;;1;4;"
ArrStationlist.[3].[3] = "GRAZ;RJ65;;1;2;"
ArrStationlist.[3].[4] = "GRAZ;RJ65;;1;0;"

Think this could be a start...

THX a lot!
Wolfgang

There's no "dot" in the syntax for array element access...

ArrStationlist[targetstation][asc_number]

...I don't know where you got your syntax from.

Code tags look like this by the way [​code][​/code].

As always, John nailed the answer. Even though you know C, I thought I’d implement John’s suggestion for anyone who is less familiar with structures:

#define DEBUG
#define ELEMENTS(X)  (sizeof(X) / sizeof(X[0]))

struct Route {                  // Declare the structure
  const char *stationName;
  const char *routeName;
  unsigned travelTime;
  unsigned arrivalTime;
} arrayList[] = {               // Now define an array of them...
  {"SALZBURG", "RJ65", 4, 9},
  {"SALZBURG", "RJ65", 4, 7},
  {"SALZBURG", "RJ65", 4, 4},
  {"SALZBURG", "RJ65", 4, 2},
  {"SALZBURG", "RJ65", 4, 1},
  {"SALZBURG", "RJ65", 4, 0}
};

void setup() {
  int i;
#ifdef DEBUG
  Serial.begin(9600);
  while (!Serial) // Waste time until Serial is created
    ;
#endif
  for (i = 0; i < ELEMENTS(arrayList); i++) {
    Serial.print("Destination: ");
    Serial.println(arrayList[i].stationName);
    Serial.print("      Route: ");
    Serial.println(arrayList[i].routeName);
    Serial.print("Travel time: ");
    Serial.println(arrayList[i].travelTime);
    Serial.print("    Arrival: ");
    Serial.println(arrayList[i].arrivalTime);
    Serial.println("========================");
  }
}

void loop() {
}

pcbbc:
There’s no “dot” in the syntax for array element access…

ArrStationlist[targetstation][asc_number]

…I don’t know where you got your syntax from.

Code tags look like this by the way [​code][​/code].

that was pseudocode, apologies forgot to mention.

econjack:
As always, John nailed the answer. Even though you know C, I thought I’d implement John’s suggestion for anyone who is less familiar with structures:

const char *stationName;
const char *routeName;

[/code]

why did you declare the station name and train name with pointers?
sry, not familiar with pointers…

I think I was misunderstood, creating a struct and an array is not the (big) issue :slight_smile:

As I posted above I would like to have one sequence (stationname, trainname, track, minutes counting down) within one array. - or part of an 2dim array.
so to say as “dataset”

my goal was to call (via loop or randomly) one element that holds all data for this one sequence…

hope this was understandable…

THX Wolfgang

my goal was to call (via loop or randomly) one element that holds all data for this one sequence...

It would be easy to do that with the code I posted. If you want to see the 3 element in the array, just write:

// Put before setup()

void ShowTrainData(int whichTrain)
{
    Serial.print("Destination: ");
    Serial.println(arrayList[whichTrain].stationName);
    Serial.print("      Route: ");
    Serial.println(arrayList[whichTrain].routeName);
    Serial.print("Travel time: ");
    Serial.println(arrayList[whichTrain].travelTime);
    Serial.print("    Arrival: ");
    Serial.println(arrayList[whichTrain].arrivalTime);
    Serial.println("========================");
}

// code as presented before...

//...inside loop() somewhere:

   ShowTrainData(3);                // Show the data for train #3

econjack:
my goal was to call (via loop or randomly) one element that holds all data for this one sequence…

It would be easy to do that with the code I posted. If you want to see the 3 element in the array, just write:

Hi
THX a lot for your effort, but I am afraid I was not able to explain my needs clearly…

Within my Array (that I call sequence…)
const String Arr_Salzburg[6] {“SALZBURG;RJ65;;4;9;”, “SALZBURG;RJ65;;4;7;”, “SALZBURG;RJ65;;4;4;”, “SALZBURG;RJ65;;4;2;”, “SALZBURG;RJ65;;4;1;”, “SALZBURG;RJ65;;4;0;”};

the 5th value (minutes until departure) should count down - in this example 9,7,4,2,1,0

this should run in a for-loop automatically without intervention.
The loop shuld run always from the first to the last item of the array.

I want to have several arrays - e.g. Arr_Salzburg[6], Arr_Linz[5], Arr_Vienna[3], Arr_Graz[9] etc…

all those arraynames (or names or whatever info about them) should be stored somewhere (my first intention was an additional array) e.g. {Arr_Salzburg,Arr_Linz,Arr_Vienna,Arr_Graz,…}

main goal is to call the arraynames-Array via loop or randomly and process the whole e,g, Arr_Salzburg completely from the first item to the last.

Pseudocode:

declare & fill Array_Stationnames with info = (Arr_Salzburg,Arr_Linz,Arr_Vienna,Arr_Graz)

declare Arr_Salzburg = “SALZBURG”;“RJ65”;4;1;", “SALZBURG”;“RJ65”;4;0;"
declare Arr_Linz = “LINZ”;“R17”;;3;3;", “LINZ”;“R17”;3;0;"
declare Arr_Vienna = “VIENNA”;“ICE”;“delayed”;4;9;", “VIENNA”;“ICE”;“delayed”;4;0;"

define function display_station()

[Main]
call function display_station(Array_Stationnames[1])
call function display_station(Array_Stationnames[5])
call function display_station(Array_Stationnames[2])
call function display_station(Array_Stationnames[4])

or - as variant
call function display_station(Array_Stationnames[RANDOM Number])

hope this explanation is better…

THX & regards
Wolfgang

PS: after Johns Posting i got to 2dimensional Arrays what should do the trick (hopefully)

Since you are only using the string constants for output, there is no need to wrap the string constants in String objects. Justy store the pointer:

const char * Arr_Salzburg[]
{
  "SALZBURG;RJ65;;4;9;",
  "SALZBURG;RJ65;;4;7;",
  "SALZBURG;RJ65;;4;4;",
  "SALZBURG;RJ65;;4;2;",
  "SALZBURG;RJ65;;4;1;",
  "SALZBURG;RJ65;;4;0;"
};


const char *  Arr_Linz[] =
{
  "LINZ;R17;;3;3;",
  "LINZ;R17;;3;0;"
};


const char *  Arr_Vienna[] =
{
  "VIENNA;ICE;delayed;4;9;",
  "VIENNA;ICE;delayed;4;0;"
};


const char *  Arr_Graz[] =
{
  "GRAZ;RJ65;;1;9;",
  "GRAZ;RJ65;;1;4;",
  "GRAZ;RJ65;;1;2;",
  "GRAZ;RJ65;;1;0;"
};


const char ** Arraylist[] =
{
  Arr_Salzburg,
  Arr_Vienna,
  Arr_Linz,
  Arr_Graz
};
const size_t ArraylistCount = sizeof Arraylist / sizeof Arraylist[0];
size_t ArraySizes[ArraylistCount] =
{
  sizeof Arr_Salzburg / sizeof Arr_Salzburg[0],
  sizeof Arr_Vienna / sizeof Arr_Vienna[0],
  sizeof Arr_Linz / sizeof Arr_Linz[0],
  sizeof  Arr_Graz / sizeof  Arr_Graz[0]
};




void setup()
{
  Serial.begin(115200);


  for (size_t i = 0; i < ArraylistCount; i++)
  {
    const char ** Array = Arraylist[i];
    Serial.println("List");
    for (size_t j = 0; j < ArraySizes[i]; j++)
    {
      Serial.println(Array[j]);
    }
  }
}


void loop()
{
}

Output:

List
SALZBURG;RJ65;;4;9;
SALZBURG;RJ65;;4;7;
SALZBURG;RJ65;;4;4;
SALZBURG;RJ65;;4;2;
SALZBURG;RJ65;;4;1;
SALZBURG;RJ65;;4;0;
List
VIENNA;ICE;delayed;4;9;
VIENNA;ICE;delayed;4;0;
List
LINZ;R17;;3;3;
LINZ;R17;;3;0;
List
GRAZ;RJ65;;1;9;
GRAZ;RJ65;;1;4;
GRAZ;RJ65;;1;2;
GRAZ;RJ65;;1;0;

WOW - that really works :slight_smile:

Thank you so much !!
exactly this was my goal to archive.

Now I should invest some time to understand what you did.... :slight_smile:

and afterwards blow the Dust from my C for beginners book.....

and read the chapter about pointers...

and all other C stuff in it

thanks again!

Wolfgang