Table name as variable

Discuss any general programming issues here
Post Reply
ilbarbax
Posts: 112
Joined: Thu Apr 01, 2010 6:41 pm

Table name as variable

Post 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
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Table name as variable

Post 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.
User avatar
jPV
Posts: 603
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Table name as variable

Post 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")
ilbarbax
Posts: 112
Joined: Thu Apr 01, 2010 6:41 pm

Re: Table name as variable

Post by ilbarbax »

Thanks for suggests.

Jpv hint was successful
Post Reply