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 » Scripters
Home Search
 

Re: Parsing Strings vs. Iterating Tables

Previous Thread :: Next Thread 
angelinafm is not online. angelinafm
Joined: 10 Apr 2011
Total Posts: 134
13 Aug 2012 04:55 PM
Simple questions really. Is it more efficient to use string.find() with a string or to use a for loop through a table to find out of a value is in either the table or the string. For example:

admins = {"Me","RandomScripters","Player"}
for i,v in pairs(admins) do
if blah == v then
You get the point
end
end

Or,

admins = "Me,RandomScripters,Player"
if string.find(admins,blah) then
You get the point
end

I'm guessing the strings but am not positive...
Report Abuse
Tarabukka is not online. Tarabukka
Joined: 18 Jan 2011
Total Posts: 394
13 Aug 2012 04:57 PM
The for loop through a table is more efficient, but you shouldn't worry about these kind of efficiency problems, a lot of people consider it pointless, because unless you're repeating the code hundreds of thousands of times, it won't really add much.

To simplify, the for loop is more efficient because Lua stores the table sequentially in memory (I think?), but with the string.find, it has to go through and find the commas each time rather than just going straight through the list.
Report Abuse
FPGA is not online. FPGA
Joined: 05 May 2012
Total Posts: 372
13 Aug 2012 04:58 PM
DO NOT use string.find. What if a person joins named RandomScript? String.find will return true in that case.
Report Abuse
Tarabukka is not online. Tarabukka
Joined: 18 Jan 2011
Total Posts: 394
13 Aug 2012 04:59 PM
Actually, what I said is probably wrong in this case. Both of them are probably O(n) but one is slightly less O(n)
Report Abuse
angelinafm is not online. angelinafm
Joined: 10 Apr 2011
Total Posts: 134
13 Aug 2012 04:59 PM
I wasn't really thinking one would be so inefficient it would cause massive lag, I was just wondering because I always hear people saying, "Oh no, there's a more efficient way to do that."(That being basically anything) Well, thanks for the answer. Kind of expected to be flamed and was happily surprised.
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
13 Aug 2012 05:05 PM
Neither is very efficient, it should be done like this:

local adminMap = {somePlayer = true, someotherPlayer = true, ...}
local function isAdmin(player)
return adminMap[player.Name] or false
end
Report Abuse
BlueTaslem is not online. BlueTaslem
Joined: 11 May 2008
Total Posts: 11060
13 Aug 2012 05:05 PM
If you require a comma, also (at beginning and end):

admins = {"Me","RandomScripters","Player"}
admins = "." .. table.concat(admins,"."):lower() .. "."

if admins:find("." .. name:lower() .. ".") then
...


I'm guessing that table looping will be faster for relatively small lists (ie, <6 or so), string for lists between there and maybe like 30, and then tables faster from then on.

Of course, they're both so fast it doesn't really matter which is which, since you don't need to do this checks frequently and the list should probably never exceed a few dozen, if that.
Report Abuse
Merely is not online. Merely
Joined: 07 Dec 2010
Total Posts: 17266
13 Aug 2012 05:11 PM
This is what I do:

local Admins = {
"Seranok",
"Player",
}

local function IsAdmin(player)
for _, playerName in pairs(Admins) do
if player.Name == playerName then
return true
end
end
return false
end
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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