26 posts
Location
Oregon, United States
Posted 02 December 2012 - 11:45 AM
Table Scanner
This utility allows you to scan tables so you don't have to write your own function. Arguments are below
scanTable(table table, criteria, bool returnLoc)
The first argument requires a table to search in. The second can be anything for the function to look for, like a string or a boolean. The last one is optional, and if it is true, it will return the int location the function found. Will be false if it didn't find what it was looking for.
Known bug: If there are multiple values the function found, and returnLoc is true, it will return the location of the last one it found.
EXAMPLE:
myTable={250, 100, "hello"}
print(scanTable(myTable,100,true))
would print
This is a very simple utility I have made. I hope you like it.
Make sure you save it to the apis folder!
1688 posts
Location
'MURICA
Posted 02 December 2012 - 01:53 PM
This is a nice little snippet. But it could be shorter.
function scanTable(table, search)
for i=1,#table do
if table[i]==search then
return i
end
end
end
The script stops as soon as it finds the first instance of search, because the return keyword doesn't allow the function to continue.
The last "int" argument isn't really necessary, since it's easy to check whether or not you've found something just by doing
if scanTable(someTable, aValue) then
Also, this would probably be more helpful if you allowed the user to say where they wanted to begin searching in the table, so that you could possibly find more than just the first match. That and if the function could find table elements that aren't numbered.
149 posts
Posted 03 December 2012 - 08:04 PM
[note i say this without looking at code since i have an idea of how it could be working]
perhaps by saving the index's of where the search finds the instances in a temporary table or local etc and then returns the index's of that search
IE:
myTable={250, 100, "hello", 100, 2}
100 is on index's 2;4 so :
print(scanTable(myTable,100,true))
would do either
2 4 [this is so that its like : instance1 .. " " .. instance2] etc
OR
2
4
-end
this resembles how the string.find() works if im not mistaken.. thats what gave me the idea :)/>