Is it possible to get the index of a table that an element is in?

Say I had this:

[lua]

mytable = {}

mytable[1]={name=“joe”}

mytable[2]={name=“bob”}

somethingelse=mytable[2]

[/lua]

Would it be possible to figure out that “somethingelse” references the second element in the table by just using “somethingelse”?

It’s not possible with what you have defined. “somthingelse” will contain the contents of mytable[2] with no reference to mytable or it’s index. You could add another element in the table stored in mytable[2] to indicate the index number.

I understand.

Thanks.

mytable[2] contains a table…somethingelse references that same table…table addresses are unique (for their duration)…so…

for i=1,#mytable do -- this will compare the actual table reference addresses not their values -- for example: print(mytable[2], somethingelse) --\> table: 00000123ABC table: 00000123ABC if mytable[i] == somethingelse then print("somethingelse's index within mytable is " .. i) end end

…but you’d be better off adding some type of additional “pointer” to save yourself that lookup

@dave, that only works if the data in mytable[2] is unique to that table entry. What if mytable[2] = 5?

Your example above is still comparing the content of mytable[2], which happens to contain another table in this particular case.

@tom:  i’m puzzled by your objections.  while you can’t discover mytable via somethingelse alone, you can discover somethingelse’s index within mytable if allowed to inspect both.  so, if anything, i’d expect any objection to my answer to be along the lines of:  “hah! you cheated! and used more than just somethingelse!” (emphasis to op’s question added)

but your original answer only suggested adding a single element for the index, so that approach would still need to directly access mytable also, so I only “cheated” as much as you did.  :D

(to answer the original question literally as asked, both of our answers come up short and still need the table {name=“bob”} to contain a reference back to mytable, then we could refer back to it using just  somethingelse and “discover” the index/indices - it’s not strictly necessary to store the index)

>> that only works if the data in mytable[2] is unique to that table entry

no, as written it would print the index of each entry of mytable that shares a reference to the same table as the reference stored in somethingelse, the elements of mytable need not be unique.  given the question’s context, i’m not sure why we’d expect that mytable would contain multiple references to the same table, but i would think that in such a case it would be the desired/proper outcome to expect all of the indices to be reported.

>>What if mytable[2] = 5?

not asked, not answered

>>  content of mytable[2], which happens to contain another table in this particular case

no, the table {name=“bob”} is not in fact contained within mytable[2], it exists at some other unrelated memory location.  the contents of mytable[2] are a pointer to that memory location (aka “a reference to it”).  the same is true of the contents of the variable somethingelse (it contains a reference to that same exact memory location, not a copy of the original table).  that is why it is legitimate to test those two references for equality and ask “do you both point to the same table?”.  it is a documented language feature.

anyway, thanks for the brain-teaser  :)

It seems you haven’t quite understood Tom’s speakings. He’s referring to the fact that, should the variables not have a unique address identifier doohickey (e.g. your variables are not tables, userdatas, functions, and baby rabbits or zebras as well as most other types of animals), your code will not tell if it cometh from the table.

Besides, this is probably faster than your attempt (dunno… is Corona’s indexOf function written in C++ or Lua?):

if table.indexOf(mytable, somethingelse) then print("Hippety hop!") end

The basic issue here is that you can just as easily tell Lua to check for a plain raw number (not the element from the table):

if table.indexOf(mytable, 5) then print("Hippety hop!") end

Or a string:

if table.indexOf(mytable, "padded sofa") then print("Hippety hop!") end

Or a Boolean:

if table.indexOf(mytable, true) then print("Hippety hop!") end

And there’s no way to know if the particular thing you checked for came from the table or is just a doppelganger posing as something from the table so that it can take over the country. There is no “Hi, I’m 5 and I’m from the table” vs. “Hi, I’m 5 and I’m just a boring other-thing”. Your approach works only half of the time. Which is, I suppose, better than none, but Tom, being a forum moderator and Corona guy, wanted to give myleggguy721 an approach that works for everything, and, because of the fundamentals of Lua itself, this is simply  not possible. (Cue the goat going “Bleeeeh!” and the tuba)

Plus, Tom was very polite and you weren’t quite so polite… I believe most people here are just trying to help. 'Course, if you didn’t mean to be impolite, by all means say. It came across that way.

Violets and all that jazz,

~ Idyllic

P.S. If you have a daughter you should watch The Little Mermaid with her sometime. If you have a son, The Lion King. If you have neither, give it up and go watch LOTR or Star Wars by yourself. All good movies. Personally, I like the Star Wars movies better than LOTR. What about you?

apologies if it came across impolitely, wasn’t my intent.  i just didn’t understand why tom objected to an answer offered that answered the op’s question.  the op’s original question was to identify the index of a table, which DO have addresses.  i wrote it out instead longhand to demonstrate the underlying principle, use indexOf if you’d rather.  and tom’s approach won’t work for everything - you can’t store an additional index on a boolean/string/number/etc, it has to be a table/userdata.  besides tables are mutable, and stored indexes can fail, thus the use care for on-demand discovery via indexOf in the first place, but that’s irrelevant and far afield from the original question.   i made no claim i was answering every possible “how do i find this in a table” -type question, just the op’s specific original question.

I guess my answer was more of a generic answer to “how can I tell where this value came from” instead answering the original question directly. I know people read these answers and try to apply it to their own situation. @Dave your answer does solve the problem in certain situations.

It’s not possible with what you have defined. “somthingelse” will contain the contents of mytable[2] with no reference to mytable or it’s index. You could add another element in the table stored in mytable[2] to indicate the index number.

I understand.

Thanks.

mytable[2] contains a table…somethingelse references that same table…table addresses are unique (for their duration)…so…

for i=1,#mytable do -- this will compare the actual table reference addresses not their values -- for example: print(mytable[2], somethingelse) --\> table: 00000123ABC table: 00000123ABC if mytable[i] == somethingelse then print("somethingelse's index within mytable is " .. i) end end

…but you’d be better off adding some type of additional “pointer” to save yourself that lookup

@dave, that only works if the data in mytable[2] is unique to that table entry. What if mytable[2] = 5?

Your example above is still comparing the content of mytable[2], which happens to contain another table in this particular case.

@tom:  i’m puzzled by your objections.  while you can’t discover mytable via somethingelse alone, you can discover somethingelse’s index within mytable if allowed to inspect both.  so, if anything, i’d expect any objection to my answer to be along the lines of:  “hah! you cheated! and used more than just somethingelse!” (emphasis to op’s question added)

but your original answer only suggested adding a single element for the index, so that approach would still need to directly access mytable also, so I only “cheated” as much as you did.  :D

(to answer the original question literally as asked, both of our answers come up short and still need the table {name=“bob”} to contain a reference back to mytable, then we could refer back to it using just  somethingelse and “discover” the index/indices - it’s not strictly necessary to store the index)

>> that only works if the data in mytable[2] is unique to that table entry

no, as written it would print the index of each entry of mytable that shares a reference to the same table as the reference stored in somethingelse, the elements of mytable need not be unique.  given the question’s context, i’m not sure why we’d expect that mytable would contain multiple references to the same table, but i would think that in such a case it would be the desired/proper outcome to expect all of the indices to be reported.

>>What if mytable[2] = 5?

not asked, not answered

>>  content of mytable[2], which happens to contain another table in this particular case

no, the table {name=“bob”} is not in fact contained within mytable[2], it exists at some other unrelated memory location.  the contents of mytable[2] are a pointer to that memory location (aka “a reference to it”).  the same is true of the contents of the variable somethingelse (it contains a reference to that same exact memory location, not a copy of the original table).  that is why it is legitimate to test those two references for equality and ask “do you both point to the same table?”.  it is a documented language feature.

anyway, thanks for the brain-teaser  :)

It seems you haven’t quite understood Tom’s speakings. He’s referring to the fact that, should the variables not have a unique address identifier doohickey (e.g. your variables are not tables, userdatas, functions, and baby rabbits or zebras as well as most other types of animals), your code will not tell if it cometh from the table.

Besides, this is probably faster than your attempt (dunno… is Corona’s indexOf function written in C++ or Lua?):

if table.indexOf(mytable, somethingelse) then print("Hippety hop!") end

The basic issue here is that you can just as easily tell Lua to check for a plain raw number (not the element from the table):

if table.indexOf(mytable, 5) then print("Hippety hop!") end

Or a string:

if table.indexOf(mytable, "padded sofa") then print("Hippety hop!") end

Or a Boolean:

if table.indexOf(mytable, true) then print("Hippety hop!") end

And there’s no way to know if the particular thing you checked for came from the table or is just a doppelganger posing as something from the table so that it can take over the country. There is no “Hi, I’m 5 and I’m from the table” vs. “Hi, I’m 5 and I’m just a boring other-thing”. Your approach works only half of the time. Which is, I suppose, better than none, but Tom, being a forum moderator and Corona guy, wanted to give myleggguy721 an approach that works for everything, and, because of the fundamentals of Lua itself, this is simply  not possible. (Cue the goat going “Bleeeeh!” and the tuba)

Plus, Tom was very polite and you weren’t quite so polite… I believe most people here are just trying to help. 'Course, if you didn’t mean to be impolite, by all means say. It came across that way.

Violets and all that jazz,

~ Idyllic

P.S. If you have a daughter you should watch The Little Mermaid with her sometime. If you have a son, The Lion King. If you have neither, give it up and go watch LOTR or Star Wars by yourself. All good movies. Personally, I like the Star Wars movies better than LOTR. What about you?

apologies if it came across impolitely, wasn’t my intent.  i just didn’t understand why tom objected to an answer offered that answered the op’s question.  the op’s original question was to identify the index of a table, which DO have addresses.  i wrote it out instead longhand to demonstrate the underlying principle, use indexOf if you’d rather.  and tom’s approach won’t work for everything - you can’t store an additional index on a boolean/string/number/etc, it has to be a table/userdata.  besides tables are mutable, and stored indexes can fail, thus the use care for on-demand discovery via indexOf in the first place, but that’s irrelevant and far afield from the original question.   i made no claim i was answering every possible “how do i find this in a table” -type question, just the op’s specific original question.

I guess my answer was more of a generic answer to “how can I tell where this value came from” instead answering the original question directly. I know people read these answers and try to apply it to their own situation. @Dave your answer does solve the problem in certain situations.