Page 1 of 1

Any way to get access to parent table?

Posted: Sat Apr 04, 2015 7:14 pm
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?

Re: Any way to get access to parent table?

Posted: Sun Apr 05, 2015 12:19 am
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.

Re: Any way to get access to parent table?

Posted: Sun Apr 05, 2015 8:17 am
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.

Re: Any way to get access to parent table?

Posted: Sun Apr 05, 2015 8:59 am
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.