manawydan Posted March 14, 2016 Share Posted March 14, 2016 so, i am try get the first table index checking with value function table.getIndexByValue(t,value) for i=1,#t do if(type(t[i])=="table")then local rila = t[i] for j=1,#t[i] do if(rila[j]==value)then return i -- return the first index end end end if(t[i]==value)then return i -- return the first index end end end i make this function but dont work with sub-table only with table how i can make the function check tables inside tables(sub-table)? Link to comment
Moderators Citizen Posted March 14, 2016 Moderators Share Posted March 14, 2016 Sorry but you didn't provide enough information. What the function exacltly should do ? Can you provide 2 or more examples like: "if I have a table like this and I call the function like this I want the function to return that." Why do you need an "index" in your situation ? Link to comment
ALw7sH Posted March 14, 2016 Share Posted March 14, 2016 You've done that already don't you? but you've wrote i instead of j in line 7 Link to comment
RenanPG Posted March 14, 2016 Share Posted March 14, 2016 I'm not sure how to do it correctly, but you need a recursion to loop through the sub tables. Something like this below: function table.getIndexByValue(t,value) for i,v in pairs(t) do -- or ipairs, if count zero if(v == value) then return i elseif(type(v) == "table") then return table.getIndexByValue(v,value) -- recursion, call the same function for internal tables end end end Link to comment
Enargy, Posted March 14, 2016 Share Posted March 14, 2016 if the values going to have the same value? Link to comment
manawydan Posted March 14, 2016 Author Share Posted March 14, 2016 so i am want do this: local weaponsTable = { {name="Soldier",weaponsID={31,22,16},weaponsAmmo = {1000,50,1}}, {name="Soldier2",weaponsID={31,22,16},weaponsAmmo = {2000,100,2}} } function giveWeaponsByLevel(player) local armyLevel = "Soldier" -- first index(1) local index = table.getIndexByValue(weaponsTable,armyLevel) -- index should be 1 local name,weaponID,weaponsAmmo = unpack(weaponsTable[index]) -- name should be "Soldier", weaponID should be weaponsID={31,22,16}, -- weaponsAmmo should be weaponsAmmo = {1000,50,1} -- so give weapons by the weaponsTable[index] for i=1,#weaponID do giveWeapon(player,weaponID[i],weaponsAmmo[i]) end end function table.getIndexByValue(t,value) for i,v in pairs(t) do -- or ipairs, if count zero if(v == value) then return i elseif(type(v) == "table") then return table.getIndexByValue(v,value) -- recursion, call the same function for internal tables end end end but index return nil instead 1 Link to comment
t3wz Posted March 14, 2016 Share Posted March 14, 2016 I'm sorry for going out of the topic, But why you don't indent your codes @manawydan? Really, they look terrible that way. Link to comment
manawydan Posted March 15, 2016 Author Share Posted March 15, 2016 because i use notepad Link to comment
Moderators Citizen Posted March 15, 2016 Moderators Share Posted March 15, 2016 Alright, then as a quick solution, I would add the "propertyName" for the function to know where to check the value in: local weaponsTable = { {name="Soldier",weaponsID={31,22,16},weaponsAmmo = {1000,50,1}}, {name="Soldier2",weaponsID={31,22,16},weaponsAmmo = {2000,100,2}} } function giveWeaponsByLevel(player) local armyLevel = "Soldier" -- first index(1) local index = table.getIndexByValue(weaponsTable, "name", armyLevel) -- index should be 1 local name, weaponID, weaponsAmmo = unpack(weaponsTable[index]) for i=1,#weaponID do giveWeapon(player, weaponID[i], weaponsAmmo[i]) end end function table.getIndexByValue(t, propertyName, value) for i, v in ipairs(t) do if v[propertyName] == value then return i end end end But a cleaner version (not saying it's the best) would be to something more like the following: local weaponsTable = { ["Soldier"] = { { id = 31, ammos = 1000 }, { id = 22, ammos = 50 }, { id = 16, ammos = 1 } }, ["Soldier2"] = { { id = 31, ammos = 2000 }, { id = 22, ammos = 100 }, { id = 16, ammos = 2 } } } function giveWeaponsByLevel( player ) local armyLevel = "Soldier" local weapons = weaponsTable[armyLevel] for k, weapon in ipairs(weapons) do giveWeapon(player, weapon.id, weapon.ammos) end end Link to comment
manawydan Posted March 15, 2016 Author Share Posted March 15, 2016 thanks man works fine! Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now