Jump to content

How to find something from table


tigerman

Recommended Posts

Hello dear community members, i need some help here

i have table but i need to find from there single word, for example "car-infernus"

could someone tell me function for that ? like to find from string i need string.find but what about tables ?

-- Thanks

Link to comment
  • Moderators

Hi,

There are no function for that ( except the string.find like you said )

You have to create a loop in which the server get 1 by 1 a word from the table then make a string.find if in a loop it return true, you can break the loop and make your instructions.

Put here your table ( all we need for understand the table )

Link to comment
function test (thePlayer,commandname,map1) 
local mapTable = {} 
for i, maps in ipairs(exports.mapmanager:getMapsCompatibleWithGamemode(getResourceFromName("race"))) do 
table.insert(mapTable, getResourceInfo(maps, "name")) 
end 
end 
addCommandHandler("test",test) 

and now it has to find map from mapTable with that name what was in command

Link to comment
  • Moderators

ok try this but what do you want to do exactly ?

function foundMap( thePlayer, commandName, map ) 
    if map then 
        local mapName = tostring( map ) 
        for k,i in ipairs ( mapTable ) do 
            outputChatBox( "if "..i[1].." == "..mapName, thePlayer ) 
            if ( tostring(i[1]) == tostring(mapName) ) then 
                outputChatBox( "I found ! It's "..tostring(i[1]), thePlayer, 180, 0, 0 ) 
                break 
            end 
        end 
    else 
        outputChatBox("/test2 [mapName]" ) 
    end 
end 
addCommandHandler("test2",foundMap) 

Link to comment
function table.find(tabl,word) 
if type(tabl) ~= "table" or word == nil then 
return false 
else 
local ret = false 
for k,v in pairs(tabl) do 
    if v == word then 
    return k 
    end 
end 
end 
end 
  

Now you can use: table.find(yourtable,whatfind) :D

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