Page 1 of 1

Table name as variable

Posted: Fri Jun 26, 2020 5:29 pm
by ilbarbax
Hi,

I want to do something like this:

Table1={1,2,3,4...
Table2={a,b,c,d,e..
Table3={d1,d2,d3,d4...

Table_I_wan_to_use$="Table2"

Dosomethngwithtable(Table_I_wan_to_use$)

Function Dosomethngwithtable (tablename$)

now I can't find a way to access i.e to table2[2] via tablename$ param

endfunction

Re: Table name as variable

Posted: Fri Jun 26, 2020 9:07 pm
by Bugala
Is this what you are after?

Code: Select all

table1 = {1, 2, 3}
table2 = {"a", "b", "c"}

TableIWant = table2

DebugPrint(TableIWant[1])

And to explain the difference.

TableIWant = "table2"

means that TableIWant is not a table, but is a string containing characters "table2"

By leaving quotes out however, the TableIWant = Table2, means that TableIWant becomes same as variable Table2, which happens to be a table.

Notice also that TableIWant is NOT a COPY of a table2, but it is a REFERENCE to Table2, so whatever you do to TableIWant, will also happen to table2.

if you wish to make INDEPENDENT COPY of table2, then you need to use CopyTable() - command to do it.

and example:

Code: Select all

table1 = {1, 2, 3}
table2 = {"a", "b", "c"}

TableIWant = table2

DebugPrint(TableIWant[1])

TableIWant[1] = 5

DebugPrint(Table2[1])
Notice How I changed TableIWant[1] value, and it also affected table2[1] value, since they are now practically same thing, as TableIWant is a reference or a pointer (not sure which one it is) to table2.

To put it short, TableIWant is accespoint to table2, unless changed to become access point to something else, for example:
TableIWant = table1

after which everything done to TableIWant, will also affect table1, but not table2 anymore.

So TableIWant used this way is always an access point to another table, which however can change to whom it works as an access point.

Re: Table name as variable

Posted: Sat Jun 27, 2020 3:21 pm
by jPV
How about putting your tables inside one main table (better use lowercase names to avoid troubles)?

Code: Select all

tables={
    table1={1,2,3},
    table2={"a","b","c"},
    table3={"d1","d2","d3"}
}


Table_I_wan_to_use$="table2"

DebugPrint(tables[Table_I_wan_to_use$][0])

Function Dosomethngwithtable(tablename$)
    ;now I can't find a way to access i.e to table2[2] via tablename$ param
    DebugPrint(tables[tablename$][2])
EndFunction

Dosomethngwithtable("table2")

Re: Table name as variable

Posted: Mon Jun 29, 2020 8:05 pm
by ilbarbax
Thanks for suggests.

Jpv hint was successful