62 posts
Location
Somewhere over the rainbow
Posted 18 November 2012 - 03:26 AM
I'm trying to figured out why tables are helpful and why should i learn them. I tryed to use them, but i'm pretty sure i'm not doing it the right way.
Here's what i know : to do a table you do
t = {the stuff, other stuff, random stuff}
[code/]
so for the sake of not being random: should i need a table for that situation? How do i use it????????
[code]
--it's a googling engine(lkinda)
function finder()
screen()
local food = {"cake", "bread", "pork", "steak", "sugar", "chicken"}
local tool = {"armor", "pickaxe", "shovel", "axe", "sword", "other"}
if input1 == food.cake then
print("work")
end
end
thanks for answering this noobish question
EDIT: I've look through every tutorial i found, still confused
715 posts
Posted 18 November 2012 - 03:52 AM
http://www.lua.org/pil/2.5.htmlIf you're still confused after why one would want to use tables, then it might be a little bit more helpful if you could specify exactly why you're pondering the question to begin with? That way it might be a bit easier to tell you why you'd want to use tables in a specific situation, rather than not.
It always depends on the need of a situation.
2088 posts
Location
South Africa
Posted 18 November 2012 - 04:56 AM
Try this
t = {the stuff, other stuff, random stuff}
food = {"cake", "bread", "pork", "steak", "sugar", "chicken"}
tool = {"armor", "pickaxe", "shovel", "axe", "sword", "other"}
--it's a googling engine(lkinda)
function finder()
screen()
for i = 1, #food do
if input1 == food[i] then
print("Worked! You entered: "..input1.." which is in the food table!")
break
end
end
end
62 posts
Location
Somewhere over the rainbow
Posted 18 November 2012 - 05:44 AM
Try this
t = {the stuff, other stuff, random stuff}
food = {"cake", "bread", "pork", "steak", "sugar", "chicken"}
tool = {"armor", "pickaxe", "shovel", "axe", "sword", "other"}
--it's a googling engine(lkinda)
function finder()
screen()
for i = 1, #food do
if input1 == food[i] then
print("Worked! You entered: "..input1.." which is in the food table!")
break
end
end
end
Ok thanks, it's a bit more precise on how to use it.
http://www.lua.org/pil/2.5.htmlIf you're still confused after why one would want to use tables, then it might be a little bit more helpful if you could specify exactly why you're pondering the question to begin with? That way it might be a bit easier to tell you why you'd want to use tables in a specific situation, rather than not.
It always depends on the need of a situation.
it's helping but my situation need for each "variable(if i can call them so)" to have a description on each. Here would be the material it need to build that specific item. So if
input = read() -- here i write cake
I'd like to read the material list of the cake (that i would enter in a variable i guess????)
thanks, if i'm not precise enought tell me
1054 posts
Posted 18 November 2012 - 06:00 AM
In that case, you could do this:
local recipes = {}
recipes.cake = "eggs + milk + sugar"
recipes.tnt = "sand + gun powder"
local input = read()
local recipe = recipes[input]
if recipe then
print("Recipe for '"..input.."' is: "recipe)
else
print("Recipe for '"..input.."' not found!")
end
Of course, you wouldn't put just a descriptive string for the recipes.. You could make it another table with ingredients, or just 3 strings or something for the crafting. Be creative. :)/>/>
62 posts
Location
Somewhere over the rainbow
Posted 18 November 2012 - 06:26 AM
Would it be possible to add multiple function to a table, as for exemple :
input = read()
t = [ cake, apple, bread]
function bread()
term.setCursorPos(10, 1)
print("You'll need 3 wheat")
print("Add all the ingredient on the same row")
end
recipe = t[ input ]
if recipe then
t[input]--i guess it's the code that would be there
end
I've put only one function for the sake of the exemple.
1054 posts
Posted 18 November 2012 - 06:46 AM
You did make some mistakes in your code (like using [ ] for some reason). Anyway, you could do it like this:
local recipes = {}
recipes.cake = function()
-- cake function
end
recipes.tnt = function()
-- tnt function
end
local input = read()
local recipeFunc = recipes[input]
if recipeFunc then
recipeFunc()
else
print("Recipe for '"..input.."' not found!")
end
62 posts
Location
Somewhere over the rainbow
Posted 18 November 2012 - 06:55 AM
You did make some mistakes in your code (like using [ ] for some reason). Anyway, you could do it like this:
local recipes = {}
recipes.cake = function()
-- cake function
end
recipes.tnt = function()
-- tnt function
end
local input = read()
local recipeFunc = recipes[input]
if recipeFunc then
recipeFunc()
else
print("Recipe for '"..input.."' not found!")
end
For the bracket, my bad :)/>/> .
Thanks it's much more clearer now, i'll try to use them in my code.
62 posts
Location
Somewhere over the rainbow
Posted 18 November 2012 - 09:43 AM
Ok next question, is it possible to search through multiple table at the same time something like :
local recipe = {food, tool, smelting}
local food = {}
food.cake = cake()
local tool = {}
tool.shovel = shovel()
local smelting = {}
smelting.ore = ore()
function function() --just to get the general idea
input = read()
find = recipe[input]
if find then
find()
end
Did i get it right ??
1054 posts
Posted 18 November 2012 - 10:20 AM
Ok next question, is it possible to search through multiple table at the same time something like :
local recipe = {food, tool, smelting}
local food = {}
food.cake = cake()
local tool = {}
tool.shovel = shovel()
local smelting = {}
smelting.ore = ore()
function function() --just to get the general idea
input = read()
find = recipe[input]
if find then
find()
end
Did i get it right ??
No. :D/>/> It would be more like:
local food = {}
food.cake = function () end
local tool = {}
tool.shovel = function () end
local smelting = {}
smelting.ore = function () end
local recipe = {food, tool, smelting}
function main() --can't be called function :)/>/>
input = read()
for i,v in pairs(recipe) do
find = recipe[v][input]
if find then
find()
break
end
end
Better way to do this, is just using my previous example, but adding a 'type' value to each recipe (tool, food, smelting,…). So you still have one big table and you still can check what category it is in.
62 posts
Location
Somewhere over the rainbow
Posted 18 November 2012 - 10:39 AM
Ok next question, is it possible to search through multiple table at the same time something like :
local recipe = {food, tool, smelting}
local food = {}
food.cake = cake()
local tool = {}
tool.shovel = shovel()
local smelting = {}
smelting.ore = ore()
function function() --just to get the general idea
input = read()
find = recipe[input]
if find then
find()
end
Did i get it right ??
No. :D/>/> It would be more like:
local food = {}
food.cake = function () end
local tool = {}
tool.shovel = function () end
local smelting = {}
smelting.ore = function () end
local recipe = {food, tool, smelting}
function main() --can't be called function :)/>/>
input = read()
for i,v in pairs(recipe) do
find = recipe[v][input]
if find then
find()
break
end
end
Better way to do this, is just using my previous example, but adding a 'type' value to each recipe (tool, food, smelting,…). So you still have one big table and you still can check what category it is in.
Wow that's perfect, you're really helping me out with this, very appreciated.
62 posts
Location
Somewhere over the rainbow
Posted 18 November 2012 - 11:07 AM
I guess it will be easier to aak question with the proper problem. I've copied parts of your code in an attempt to understand each part while trying to run it and modify it, but i only crashed into error at line 57
so here's my code for dico program:
sizeX, sizeY = term.getSize()
function screen()
term.clear()
term.setCursorPos(14, 1)
print("Minecraft recipe guide")
end
-- TABLES
local food = {}
food.cake = function()
term.clear()
print("worked")
screen()
end
local tool = {}
tool.shovel = function() end
local smelting = {}
smelting.ore = function() end
local recipe = {food, tool, smelting}
function askBox()
local a = "----------------"
term.setCursorPos(sizeX - 16, 3)
print(a)
end
function ask()
screen()
term.setCursorPos(sizeX -25 , 2)
print("Search : ")
askBox()
term.setCursorPos(sizeX - 16, 2)
input1 = read()
sleep(0.5)
term.clear()
term.setCursorPos(((sizeX / 2) - 10), (sizeY / 2))
textutils.slowPrint("Looking for "..input1, 18)
sleep(1)
return input1
end
function finder()
screen()
for i,v in pairs(recipe) do
find = recipe[v][input] --line 57
if find then
find()
break
end
end
end
function main()
screen()
ask()
finder()
end
main()
when i try to run it CC tell's me : dico:57: attempt to index ? (a nil value)
My bet you'd be that the problem come's from that variable "v" since it's undefined. I don't know how to define it, i've tryed but still got that error.
Any advice?
1688 posts
Location
'MURICA
Posted 18 November 2012 - 11:20 AM
"v" is definitely defined, "recipe[v]" probably isn't. You should check if recipe[v] exists, before trying to find recipe[v][input].
Scratch that, evidently, "v" is "recipe[v]", so you would just do v[input].
1054 posts
Posted 18 November 2012 - 11:35 AM
Yes, I messed up there. :)/>/> v represents one of the three tables in the table recipe.