|
| 23 Aug 2014 01:40 PM |
How do you find a value in a table efficiently? Im going to have a table with over 600 values. I need an efficient way to find a value quickly.
Example:
tbl = {"apple", "pear", "pineapple", "avocado", "orange", "banana"}
Is it possible I can just skip to the value banana without checking if apple,pear,pineapple, etc. isn't banana? |
|
|
| Report Abuse |
|
|
|
| 23 Aug 2014 01:49 PM |
You could skip to a point instantly if you stored the values as keys (tbl = {["apple"] = "applevalue", etc}) However, if you want to keep them as values, there's a few ways of going about it:
1. Use pairs or ipairs to iterate through the table. E.g.: local function FruitFind(val) do for _, fruit in pairs(tbl) do if fruit == val then return fruit end end return nil end
2. Use a fast search algorithm like the binary search. This is way harder and probably wouldn't be worth it if your table contains less than a few hundred values. |
|
|
| Report Abuse |
|
|
Mitko0o1
|
  |
| Joined: 30 Nov 2010 |
| Total Posts: 5725 |
|
|
| 23 Aug 2014 01:52 PM |
| http://wiki.roblox.com/index.php?title=Function_dump/Table_manipulation |
|
|
| Report Abuse |
|
|
|
| 23 Aug 2014 01:57 PM |
Oops, I didn't read your whole post--so you probably would want to use binary search.
local tbl = {"apples", "pears", "blah blah blah"} local function BinarySearch(val) local left, right = 1, #tbl while left <= right do local middle = math.floor((left + right)/2) if tbl[middle] == val then return middle elseif tbl[middle] > val then right = middle - 1 else left = middle + 1 end end end print(BinarySearch("pears")) -- outputs 2 print(BinarySearch("doesntexist")) -- outputs nil |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Aug 2014 03:52 PM |
Nah just use them as keys.
local values = {}; values.apple = true; values.banana = true;
if values["BLAH"] then print("Exists, no loop needed and is very efficient"); end |
|
|
| Report Abuse |
|
|
|
| 23 Aug 2014 06:28 PM |
So what I did is that I made my 600 values into keys. I assigned them an integer value not a boolean though. I like the idea of not using a loop and instantly finding the value however.Kind of like a Findfirstchild method for a table. Here's an example:
tbl = {["apple"] = 1, ["pear"] = 2, ["pineapple"] = 3}
Is there anyway to find pineapple without iterating through the whole thing? If not, what's the best way? |
|
|
| Report Abuse |
|
|
|
| 23 Aug 2014 06:30 PM |
Trying to take lag and time into account since there are over 600 keys in my real table
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
|
| 23 Aug 2014 06:39 PM |
K so last thing, is this right?
Local c = math.random (1, #tbl)
function create () if c = tbl.pineapple then print ("blah") end end
(Is there a way I can use the value? Like if c = 3 then instead of the one above?) |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Aug 2014 06:43 PM |
if c == tbl.pineapple then
|
|
|
| Report Abuse |
|
|
|
| 23 Aug 2014 06:52 PM |
I meant could you do something like c = 3? Instead?
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Aug 2014 06:53 PM |
No, but you can do: tbl.pineapple = 3 to set it to 3.
|
|
|
| Report Abuse |
|
|