generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Searching for a value in a table

Previous Thread :: Next Thread 
LegendOfPhantom is not online. LegendOfPhantom
Joined: 27 Apr 2014
Total Posts: 101
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
SuperAlpacaMan is not online. SuperAlpacaMan
Joined: 18 Nov 2008
Total Posts: 774
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 is not online. 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
SuperAlpacaMan is not online. SuperAlpacaMan
Joined: 18 Nov 2008
Total Posts: 774
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 is not online. 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
LegendOfPhantom is not online. LegendOfPhantom
Joined: 27 Apr 2014
Total Posts: 101
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
LegendOfPhantom is not online. LegendOfPhantom
Joined: 27 Apr 2014
Total Posts: 101
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 is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
23 Aug 2014 06:31 PM
tbl.pineapple
Report Abuse
LegendOfPhantom is not online. LegendOfPhantom
Joined: 27 Apr 2014
Total Posts: 101
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 is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
23 Aug 2014 06:43 PM
if c == tbl.pineapple then
Report Abuse
LegendOfPhantom is not online. LegendOfPhantom
Joined: 27 Apr 2014
Total Posts: 101
23 Aug 2014 06:52 PM
I meant could you do something like c = 3? Instead?

Report Abuse
cntkillme is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image