Any way to get access to parent table?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Any way to get access to parent table?

Post by Bugala »

I have this situation which I didnt think well enough, and i woudl need to redo a lot of code or figure out some hacks possibly in future to get around this problem, unless i can somehow access the parent.

I have this:

Code: Select all

mytable = {
           [1] = { 1, 2, 3},
           [2] = { 4, 5, 6}
               }
Problem is, that I am accessing function in way of:

Code: Select all

myfunction(mytable[1])
And now after having wrote lot of code inside the myfunction using it based upon it receiving the table, i now suddenly realise I would actually need to know which number table it sent there.

Hence, is it possible to do something like:

Code: Select all

Function myfunction(table)
index = tablesindex(table)
and it would then return for example number 1 to "index" variable?
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to get access to parent table?

Post by Bugala »

I was able to figure out a simple solution to this problem. However, I am still interested wether accessing parent tables and knowing of which index you are using of that table is possible. It could be useful sometime.
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Any way to get access to parent table?

Post by jalih »

Bugala wrote: Problem is, that I am accessing function in way of:

Code: Select all

myfunction(mytable[1])
And now after having wrote lot of code inside the myfunction using it based upon it receiving the table, i now suddenly realise I would actually need to know which number table it sent there.
How about making things a a little bit easier:

Code: Select all

myfunction(table, index)
Now your function knows the table and index key.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to get access to parent table?

Post by Bugala »

Great idea. I will start using that system in future.

Problem is that theres already loads of lines of code at this point, so many time doing changes is lot of trouble.

Like in this specific case to which i was asking for, this same function have been called many times elsewhere in program, hence changing it to this version, would mean changing all the calls as well.

But great idea that system of yours. Will definetily start using that system in future.
Post Reply