Jump to content

get index by value in sub-table


manawydan

Recommended Posts

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

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

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

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
  • Moderators

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...