Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Is it possible to get the index of a table that an element is in?
Started by mylegguy721 Aug 26 2015 08:24 AM

- - - - -
8 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

mylegguy721

[GLOBAL: userInfoPane.html]
mylegguy721
  • Enthusiast

  • 44 posts
  • Corona SDK

Say I had this:

mytable = {}
mytable[1]={name="joe"}
mytable[2]={name="bob"}
somethingelse=mytable[2]

 

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



[TOPIC: post.html]
#2

Tom

[GLOBAL: userInfoPane.html]
Tom
  • Moderator

  • 1,480 posts
  • Corona Staff

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.



[TOPIC: post.html]
#3

mylegguy721

[GLOBAL: userInfoPane.html]
mylegguy721
  • Enthusiast

  • 44 posts
  • Corona SDK

I understand.

Thanks.



[TOPIC: post.html]
#4

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,373 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

Tom

[GLOBAL: userInfoPane.html]
Tom
  • Moderator

  • 1,480 posts
  • Corona Staff

@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.



[TOPIC: post.html]
#6

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,373 posts
  • Corona SDK

@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  :)



[TOPIC: post.html]
#7

idyllicsteam

[GLOBAL: userInfoPane.html]
idyllicsteam
  • Observer

  • 2 posts
  • Corona SDK

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?



[TOPIC: post.html]
#8

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,373 posts
  • Corona SDK

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.



[TOPIC: post.html]
#9

Tom

[GLOBAL: userInfoPane.html]
Tom
  • Moderator

  • 1,480 posts
  • Corona Staff

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.




[topic_controls]
[/topic_controls]