How would you search a table for a value? Also, I might need to know how to search tables inside of tables as well.... and also, is there any other way for turtles to compare things rather than using whatever they have in their inventory? For example, could you set dirt = 1 and wood = 2 and when the turtle mines wood it says ID = 2 do this....
How to search tables
Started by Joe3000, May 27 2013 01:20 AM
3 replies to this topic
#1
Posted 27 May 2013 - 01:20 AM
#2
Posted 27 May 2013 - 02:00 AM
You can iterate the table looking for the value. This would return the first key a specified value was found at:
Tables inside of tables is just as easy, you simply have to recurse during your search or specify the child table if you're just looking to search a single subtable.
There are no other turtle comparison methods other than between two inventory slots and between an inventory slot and a block in the world.
function findInTable(myTable, searchValue)
for k,v in pairs(myTable) do
if v == searchValue then
return k
end
end
end
Tables inside of tables is just as easy, you simply have to recurse during your search or specify the child table if you're just looking to search a single subtable.
There are no other turtle comparison methods other than between two inventory slots and between an inventory slot and a block in the world.
#3
Posted 27 May 2013 - 08:49 AM
Lyqyd, on 27 May 2013 - 02:00 AM, said:
You can iterate the table looking for the value. This would return the first key a specified value was found at:
Tables inside of tables is just as easy, you simply have to recurse during your search or specify the child table if you're just looking to search a single subtable.
There are no other turtle comparison methods other than between two inventory slots and between an inventory slot and a block in the world.
function findInTable(myTable, searchValue) for k,v in pairs(myTable) do if v == searchValue then return k end end end
Tables inside of tables is just as easy, you simply have to recurse during your search or specify the child table if you're just looking to search a single subtable.
There are no other turtle comparison methods other than between two inventory slots and between an inventory slot and a block in the world.
Absolutely right when you not use a peripheral.
For example openccsensors could be used to search thought the inventory and compare the itemname with a table for example.
Another mod that can expand the turtles abilities to read its inventory is Aperture science turtles
Greets
Loki
#4
Posted 27 May 2013 - 02:40 PM
To go about in more detail as to what Lyqyd previously stated:
The default keys in tables are in numerical order, we can obviously change that:
Default Table:
Altered table:
So before we changed the keys in table 'foo' they were assigned as 1, 2, and 3. Now the first key is "Cloudy", the second key is "Lyqyd", and the last key is "Dan200". N
ow this is all nice to know, but this is not your direct question; onward we go.
Now let's talk about elements of a table. In the above table, 'foo', we have assigned three keys: Cloudy, Lyqyd, and Dan200. The elements of these keys are as follows: cool, extremist, and awesome. Now, lets say we want to only show people who are 'cool'. We can iterate through the table and use a conditional statement to match us with people who are 'cool':
Had there been no elements with a value of "cool", it would write "Y U NO COOL!". This is a very small example of the power of tables and how beneficial they are to use. In conclusion, tables are your friends; use them and abuse them.
The default keys in tables are in numerical order, we can obviously change that:
Default Table:
local foo = {"cool", "extremist", "awesome"}
Altered table:
local foo = {["Cloudy"] = "cool", ["Lyqyd"] = "extremist", ["Dan200"] ="awesome"}
So before we changed the keys in table 'foo' they were assigned as 1, 2, and 3. Now the first key is "Cloudy", the second key is "Lyqyd", and the last key is "Dan200". N
ow this is all nice to know, but this is not your direct question; onward we go.
Now let's talk about elements of a table. In the above table, 'foo', we have assigned three keys: Cloudy, Lyqyd, and Dan200. The elements of these keys are as follows: cool, extremist, and awesome. Now, lets say we want to only show people who are 'cool'. We can iterate through the table and use a conditional statement to match us with people who are 'cool':
for k, v in pairs(foo) do
if v == "cool" then
write(k.." is cool")
else
write("Y U NO COOL!")
end
end
Had there been no elements with a value of "cool", it would write "Y U NO COOL!". This is a very small example of the power of tables and how beneficial they are to use. In conclusion, tables are your friends; use them and abuse them.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











