Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Create table
- local items = {}
- --Let's put stone ID at index "stone"
- items['stone'] = 1
- --And torch at 50
- items['torch']= 50
- -- Searching function
- local function search(table, toSearch)
- -- Check if given table is actually table
- if type(table) ~= "table" then
- -- If it's not, raise error
- error("Table expected, got "..type(table) or "nil", 2)
- end
- -- Loop trough all values in given table
- -- Set "index" to current item's index and set "value" to current item's value
- for index, value in pairs(table) do
- -- If we fount it
- if index == toSearch then
- -- Return fount value
- return value
- end
- end
- -- If we reached this point it meas that items was not fount
- -- Return false since nothing was fount
- return false
- end
- -- Some testing
- -- Search for "stone" in table "items"
- print(tostring(search(items, "stone"))) --> 1
- print(tostring(search(items, "noSuchThing"))) --> false
- -- Search inside nil
- search(nil, "stone")
Advertisement
Add Comment
Please, Sign In to add comment