This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
EveryOS's profile picture

Only get [index names] from tables

Started by EveryOS, 13 April 2016 - 06:38 PM
EveryOS #1
Posted 13 April 2016 - 08:38 PM
I have a table

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

Can I get the index names rather than the index value?
Creator #2
Posted 13 April 2016 - 08:44 PM
yes.

for i,v in pairs(tabl)
– i is index
end
EveryOS #3
Posted 13 April 2016 - 08:45 PM
yes.

for i,v in pairs(tabl)
– i is index
end
Thanks
Sewbacca #4
Posted 13 April 2016 - 09: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