Jump to content




Only get [index names] from tables


  • You cannot reply to this topic
3 replies to this topic

#1 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 13 April 2016 - 07:38 PM

I have a table

{
['i']=0,
['o'] = 1
}

Can I get the index names rather than the index value?

#2 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 13 April 2016 - 07:44 PM

yes.

for i,v in pairs(tabl)
-- i is index
end

#3 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 13 April 2016 - 07:45 PM

 Creator, on 13 April 2016 - 07:44 PM, said:

yes.

for i,v in pairs(tabl)
-- i is index
end
Thanks

#4 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 13 April 2016 - 08:07 PM

You can save code:

function getIndex(tab, value)
  local indexes = {} 
  for index, var in pairs(tab) do
    if var == value then 
      table.insert(indexes, index) 
    end 
  end 

  return table.unpack(indexes)
end






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users