This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MegaShrimp's profile picture

How would you state a person said something in a table?

Started by MegaShrimp, 18 November 2012 - 08:52 AM
MegaShrimp #1
Posted 18 November 2012 - 09:52 AM
Ok, so I have this little sample script

Items = {

{"Cookie","357"}
{"Torch"."16"}

}

def = io.read

if def ==–[[ blah blah blah ]] then
end

————————————————————-

How would I state that if what is typed is Items[1][1] or Items[2][2], without becoming specific like

if def == Items[1][1] then
end
if def == Items[2][2]
end
————————————————————–


I figure ipairs or inpairs would be involved but I'm not particularly sure…


Can anyone help me here?
KaoS #2
Posted 18 November 2012 - 09:56 AM
here

for k,v in pairs(items) do
  if def==v[1] then
    --some code here
  end
end
Orwell #3
Posted 18 November 2012 - 10:01 AM
I think in your case, you could use this:

local Items = {
["Cookie"]="357",
["Torch"]="16",
}
local def = io.read()
local itemNum = Items[def]
if itemNum then
  -- do something with that number (actually a string in you're case)
end
MegaShrimp #4
Posted 18 November 2012 - 10:15 AM
here

for k,v in pairs(items) do
  if def==v[1] then
	--some code here
  end
end


So does this also mean if a person said 16 it would perform the same function as if they said torch?
remiX #5
Posted 18 November 2012 - 10:33 AM
Yes, you can do this like this:


items = {
cookie = 357,
torch = 16
}
term.clear() term.setCursorPos(1,1)
while true do
    term.setTextColour(colours.red) write("Name/ID: ") i = string.lower(read())
    term.setTextColour(colours.yellow)
    for k,v in pairs(items) do
        if i == k then
            print("You typed in name: " .. k .. " which has ID number of: " .. v)
            break
        elseif tonumber(i) == v then
            print("You typed in ID: " .. v .. " which is named: " .. k)
            break
        end        
    end
end

Spoiler
MegaShrimp #6
Posted 18 November 2012 - 12:25 PM
Well my real goal is to just simply be able to state something if I type one of the selections given in the table. Could you dumb it down a little for me seeing as that I'm completely lost?

Its like saying


If I type a table selection or its other then
give me info of that selection I typed (Which would include some type of defining function probs..)
end
Kingdaro #7
Posted 18 November 2012 - 12:46 PM
If I'm reading you right, you're looking for sort of a dictionary, where the user inputs a word, and the program prints out the information on your word. That's actually pretty simple to do.


-- for this example, we will use fruits.
local definitions = {
	apples = "Red, sometimes sour, sometimes sweet, always juicy.";
	oranges = "Usually sour, really juicy. Can't really eat the skin.";
	grapes = "Like apples, but tiner. Really juicy, grow on a vine.";
}

-- just for convenience.
local function clearScreen(  )
	term.clear()
	term.setCursorPos(1,1)
end

while true do
	clearScreen()

	print 'What do you want to know about?'
	local input = read() -- this lets us know what the user typed

	clearScreen()

	local definition = definitions[input]
	if definition then -- check if a definition exists for the input
		print('Definition for '..input..':')
		print(definition)
	else
		print('Could not find definition for '..input)
	end

	print 'Press enter to continue.'
	read() -- just so the user can see what we have written before taking input again.
end

In this program, if you type "apples", it'll print "Red, sometimes sour, sometimes sweet, always juicy."
remiX #8
Posted 18 November 2012 - 01:51 PM
If you wanted that why did you ask
So does this also mean if a person said 16 it would perform the same function as if they said torch?

If kingdaro didn't answer what you need, then im confused
KaoS #9
Posted 18 November 2012 - 07:07 PM
I don't think it is a dictionary, I think it is an item dispenser based on ID and name
Kingdaro #10
Posted 18 November 2012 - 07:42 PM
I thought about something like that, instead of printing a definition, it would call a function.

Adapted my dictionary program:

function dispenseCookie()
	print "I'm dispensing a cookie!"
	-- code for dispensing a cookie
end

function dispenseTorch()
	print "I'm dispensing a torch!"
	-- code for dispensing torch
end

local operations = {
	Cookie = dispenseCookie,
	["357"] = dispenseCookie,

	Torch = dispenseTorch,
	["16"] = dispenseTorch
}

-- just for convenience.
local function clearScreen(  )
	term.clear()
	term.setCursorPos(1,1)
end

while true do
	clearScreen()

	print 'Dispense?'
	local input = read() -- this lets us know what the user typed

	clearScreen()

	local operation = operations[input]
	if operation then -- check if operation exists for the user input
		operation()
	else
		print 'Operation does not exist.'
	end

	print 'Press enter to continue.'
	read() -- just so the user can see what we wrote before taking input again.
end
KaoS #11
Posted 18 November 2012 - 08:10 PM
that would work fine except that even if you say nothing in the read() prompt it returns an empty string. use

if operation and operation~='' then
here is an example of using the for loop and one function


local tOps={{'cookie',357,'357'},{'torch',16,'16'}}
local function dispense(item)
  print('Oh yeah! have a '..item..'!')
  --your code here (I would advise adding something to tOpts like a redstone code etc for each item to get it. up2u)
end

while true do
  term.clear()
  term.setCursorPos(1,1)
  print('What item would you like sir? [ name | id ]')
  write('input: ')
  local input=read()
  for num,tNames in pairs(tOpts) do
   for _num,name in pairs(tNames) do
	if input==name then
	 dispense(tNames[1]) --will call the dispense function with the FIRST name specified
	end
   end
  end
end