How do I look in a folder and do a string.match to see if any of the names of the items contain a certain word.
function MatchNameInFolder(folder,text)
text = string.lower(text)
for _,v in pairs(folder:GetChildren()) do
if string.match(string.lower(v.Name),text) then
return v
end
end
end
MatchNameInFolder(Folder,'PartName')
You can learn more here:
pairs and iPairs
This would only return the first child found where the search string is contained within the child’s name. Consider using string.find() instead of string.match() as search patterns aren’t being utilised. Here’s my implementation which returns an array of all found instances.
local function FindChildrenBySearch(Folder, Search)
local Children = {}
for _, Child in ipairs(Folder:GetChildren()) do
if Child.Name:lower():find(Search:lower()) then
table.insert(Children, Child)
end
end
return Children
end
A couple of other things to note, use the ipairs() iterator when iterating over arrays as it is quicker/more efficient than the pairs() iterator and define functions locally (when they don’t need to be global), by integrating functions into the local environment they can be referenced and indexed much faster than if you were to reference and index them from the global environment instead (if they were defined globally).
I did some testing and pairs is actually faster than ipairs (given random arrays with 100k+ simulations).
Also returning the first item seems to be what they’re looking for, but yes you can also return an array. As for string.match
, I only used that because they were using it. It really doesn’t matter regardless.
Not sure how you performed your benchmarks but ipairs() will always iterate over an entire array faster than pairs can iterate over an entire dictionary (providing both are similar in length).
Old
It was a while ago, but it was this:
local number = 1000000
local array = {124,'hi',workspace}
for i=1,100 do
table.insert(array,math.random(0.01,10000))
end
local results = {}
function avg(list)
local total=0
for _,v in pairs(list) do
total+=v
end
return total/#list
end
local average = {}
for loop=1,number do
local start = tick()
local array2 = {}
for i=1,#array do
table.insert(array2,array[i])
end
table.insert(average,tick()-start)
if loop%10000==0 then
task.wait()
end
end
results['pairs']=avg(average)
local average = {}
for loop=1,number do
local start = tick()
local array2 = {}
for _,v in pairs(array) do
table.insert(array2,v)
end
table.insert(average,tick()-start)
if loop%10000==0 then
task.wait()
end
end
results['iPairs']=avg(average)
local average = {}
for loop=1,number do
local start = tick()
local array2 = {}
for _,v in ipairs(array) do
table.insert(array2,v)
end
table.insert(average,tick()-start)
if loop%10000==0 then
task.wait()
end
end
results['iteration loop']=avg(average)
table.sort(results,function(a,b) return a < b end)
local text = '\n'
for i,v in pairs(results) do
text=text..i..': '..v..'\n'
end
warn(text)
I just performed the test again, and these were the results on average:
I’ve tested multiple times and the results appear to be the same.
Actually just realized that I mislabeled them:
local number = 1000000
local array = {124,'hi',workspace}
for i=1,100 do
table.insert(array,math.random(0.01,10000))
end
local results = {}
function avg(list)
local total=0
for _,v in pairs(list) do
total+=v
end
return total/#list
end
local average = {}
for loop=1,number do
local start = tick()
local array2 = {}
for i=1,#array do
table.insert(array2,array[i])
end
table.insert(average,tick()-start)
if loop%10000==0 then
task.wait()
end
end
results['iteration loop']=avg(average)
local average = {}
for loop=1,number do
local start = tick()
local array2 = {}
for _,v in pairs(array) do
table.insert(array2,v)
end
table.insert(average,tick()-start)
if loop%10000==0 then
task.wait()
end
end
results['pairs']=avg(average)
local average = {}
for loop=1,number do
local start = tick()
local array2 = {}
for _,v in ipairs(array) do
table.insert(array2,v)
end
table.insert(average,tick()-start)
if loop%10000==0 then
task.wait()
end
end
results['iPairs']=avg(average)
table.sort(results,function(a,b) return a < b end)
local text = '\n'
for i,v in pairs(results) do
text=text..i..': '..v..'\n'
end
warn(text)
That’s my bad.
I will say though that the difference is seemingly negligible. I don’t really see the point in using iPairs
unless you’re really concerned about 100% efficiency. Plus obviously iPairs
only works with arrays.