Using two dimensional arrays


#1

having a problem understanding how ewon basic uses arrays, trying to DIM a two dimensional array type of string. I need to store the tag name and index to be able to lookup the info later in the program.
TagArray$( First Dim, Sec Dim) but I dont think thats how basic 2 works

tried, so I tried just making two arrays code below with no luck.


n% = GETSYS PRG, “NBTAGS” // Get all of tags as an int
Print "Number of Tags: " + str$(n%)
DIM TagInfo$(20,20)

FOR i% = 0 TO (n% - 1)
SETSYS TAG, “load”, -i% // -i% for index correction
t$ = GETSYS TAG, “name” // Load tagName (includes Room:Tag)
i$ = GETSYS TAG, “id” // Load Tag Index

// save tag and index for updating polled values from db

TagInfo$(i% +1,1) = t$
TagInfo$(i% +1,2) = i$

Print "Full Tag name: " + t$ + " - Tag Index: " + i$

ONCHANGE t$, “@updateServer(’ " + t$ + " ', ’ " + i$ + " ’ )”
NEXT i%


#2

Hi @wizardontherun

Arrays in BASIC are a bit strange. There doesn’t exists an array type directly for strings, though we can simulate something similar by using a char array. Below I altered your script (pasted above) to use an array. The array is formatted as:

Tag Name: index 1 to 46 (maximum tagname length of 45 characters)
Tag Index: index 47 to 50 (maximum tag index can only be 4 digits long).



Populate the tag array
So basically we could then do:

n% = GETSYS PRG, "NBTAGS"
DIM tagInfo$(n%, 50) // Create an array with enough indexes to fit all tags

FOR i% = 1 TO n%
    SETSYS TAG, "LOAD", -(i% - 1)
    t$ = GETSYS TAG, "name" // Load tagName (includes Room:Tag)
    i$ = GETSYS TAG, "id" // Load Tag Index

    tagInfo$(i%, 1 TO 46) = t$
    tagInfo$(i%, 47 TO 50) = i$
NEXT i%

PRINT ALL TAGS

FOR i% = 1 TO n%
   PRINT tagInfo$(i%, 1 TO 46)   REM Tag name
   PRINT tagInfo$(i%, 46 TO 50)  REM Tag index
NEXT i%

You could then access the tag information by doing something like: