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

how can i condense the values from a table into one value?

Started by queebles, 13 September 2014 - 02:28 AM
queebles #1
Posted 13 September 2014 - 04:28 AM
i have a table with 4 one digit numbers in it and i want to smush those 4 numbers into 1 four digit number. i don't know where to go from here.


tbl = {}

print("enter first number:") entry1 = read()
print("enter second number:") entry2 = read()
print("enter third number:") entry3 = read()
print("enter fourth number:") entry4 = read()

entry = entry1..entry2..entry3..entry4
print("this is: 'entry1..entry2..entry3..entry4'")
print(entry)

tbl[1] = entry1
tbl[2] = entry2
tbl[3] = entry3
tbl[4] = entry4

entryt = tbl[1]..tbl[2]..tbl[3]..tbl[4]
print("this is: 'tbl[1]..tbl[2]..tbl[3]..tbl[4]'")
print(entryt)

print("tostring with key and value")
for k, v in pairs(tbl) do
  print("key is:"..tostring(k).." value is:"..tostring(v))
end

a = entry
b = entryt
code = 1234
print("entry = "..entry)
print("entryt = "..entryt)
print("code = "..code)
if a == entry then print("a = entry")
  else print("a ~= entry")
end
if b == entryt then print("b = entryt")
  else print("b ~= entryt")
end
if a == b then print("a = b")
  else print("a ~= b")
end
if a == code then print("entry = code")
  else print("entry ~= code")
end
if b == code then print("entryt = code")
  else print("entryt ~= code")
end

i apologize for what i'm sure is just a mess, but i'm just learning….. anyway when i run this and enter the numbers 1,2,3,and 4 into the table and try to concatenate them into one number, they don't equal the code variable, which is 1234…. can someone help me do this please? thanks for listening :)/>
Lyqyd #2
Posted 13 September 2014 - 05:23 AM
You're making a string out of them when you concatenate them. If you tonumber() the resulting string, it may do what you want: local entry = tonumber(entry1..entry2..entry3..entry4)

What is all this for, by the way? Seems like a very odd thing to do.
queebles #3
Posted 13 September 2014 - 04:19 PM
Wow, you answered quickly, I wasn't expecting that :)/> Cool, I didn't know that tonumber() was a thing. I'll give that a try. As for what this does, most of this code was me testing…. I'm trying to make a touchscreen keypad lock, without looking at anyone else's code(for my own sense of accomplishment), that requires the user to enter 4 numbers to unlock a door. I was having trouble figuring out how to compare the user's entries to the correct unlock code. Thank you very much, I'm sure I'll be back, I've got a few more things I'm having trouble with.
Lignum #4
Posted 13 September 2014 - 04:39 PM
You can also do this, which will work no matter how many entries you have:

local tbl = { 1, 2, 3, 4 }
local num = tonumber(table.concat(tbl)) --# This will convert { 1, 2, 3, 4 } to "1234" and then to 1234.
print(num) --# Prints 1234.
queebles #5
Posted 13 September 2014 - 08:34 PM
Alright, told ya I'd be back… Thanks Lyqyd and Lignum, that was exactly what I needed. Now I'm stuck again, I'm not sure I'm using functions properly, also no matter which button I hit it only prints '1' and lets me keep pressing buttons after the first four. I'm not sure where I messed up… So here is what i have so far… http://pastebin.com/mKYJST0J


function check()
  entry = tonumber(codeTbl[1]..codeTbl[2]..codeTbl[3]..codeTbl[4])
  if entry == code then mon.setCursorPos(1,10) print("ACCESS GRANTED")  --do something here with redstone
    else mon.setCursorPos(1,10) print("ACCESS DENIED")
  end
_1st = false
_2nd = false
_3rd = false
_4th = false
codeTbl = {}
end
function _123or4() --is it the 1st, 2nd, 3rd, or 4th number you pressed
  if _1st == false then mon.setCursorPos(11,2) _1st = true
    elseif _2nd == false then mon.setCursorPos(12,2) _2nd = true
    elseif _3rd == false then mon.setCursorPos(13,2) _3rd = true
    elseif _4th == false then mon.setCursorPos(14,2) _4th = true
  end
end
mon = peripheral.wrap("left")
term.redirect(peripheral.wrap("left"))
mon.setTextScale(.5)
mon.setBackgroundColor(colors.black)
mon.clear()
codeTbl = {}  --table the code is hopefully being stored within
_1st = false
_2nd = false
_3rd = false
_4th = false
code = 1234
--read txt file and print it to monitor
hFile = fs.open("keypad.txt", "r")
for i = 1, 9 do
  print(hFile.readLine())
end
hFile.close()
--it looks like this
--/-\/-\/-\
--|1||2||3|
--\-/\-/\-/ ^^^^
--/-\/-\/-\
--|4||5||6|
--\-/\-/\-/
--/-\/-\/-\
--|7||8||9|
--\-/\-/\-/
w,h = mon.getSize()
repeat
  event,side,w,h = os.pullEvent("monitor_touch")
  if w == 1 or 2 or 3 and h == 1 or 2 or 3 then _123or4()
    print("1")
    elseif w == 4 or 5 or 6 and h == 1 or 2 or 3 then _123or4()
    print("2")
    elseif w == 7 or 8 or 9 and h == 1 or 2 or 3 then _123or4()
    print("3")
    elseif w == 1 or 2 or 3 and h == 4 or 5 or 6 then _123or4()
    print("4")
    elseif w == 4 or 5 or 6 and h == 4 or 5 or 6 then _123or4()
    print("5")
    elseif w == 7 or 8 or 9 and h == 4 or 5 or 6 then _123or4()
    print("6")
    elseif w == 1 or 2 or 3 and h == 7 or 8 or 9 then _123or4()
    print("7")
    elseif w == 4 or 5 or 6 and h == 7 or 8 or 9 then _123or4()
    print("8")
    elseif w == 7 or 8 or 9 and h == 7 or 8 or 9 then _123or4()
    print("9")
  end
i = #codeTbl
until i == 4
  if #codeTbl == 4 then check()
  end

I hope I don't have any typos, I transcribed that from minecraft
Bomb Bloke #6
Posted 14 September 2014 - 02:50 AM
I suspect it's not the only problem, but this:

if w == 1 or 2 or 3 and h == 1 or 2 or 3 then _123or4()

… doesn't do what you think it does.

You meant this:

if (w == 1 or w == 2 or w == 3) and (h == 1 or h == 2 or h == 3) then _123or4()

Which could be further shrunk down to this:

if w > 0 and w < 4 and h > 0 and h < 4 then _123or4()
queebles #7
Posted 14 September 2014 - 05:57 PM
Ahhh, yes, thank you Bomb bloke, that helped. And you're right, there are definitely other problems.
queebles #8
Posted 14 September 2014 - 07:01 PM
I've discovered something weird going on with this…. the monitor is letting me touch the screen outside of the screen. Idk if I can explain this…. the keypad is printed on the monitor at coordinates 1,1 which is good. however i can touch coordinates 0,0(top left corner of the border around the screen) which isn't even on the screen and it thinks that i hit the 1 button. in fact the whole thing is stretched up one and left one. visually its in the right place, but by touch it's messed up.. all coordinates from 0,0 - 0,9 and 0,0 - 9,0 register a touch. http://pastebin.com/BuvQtjkT
Bomb Bloke #9
Posted 15 September 2014 - 01:06 AM
Sounds a bit like this bug; ComputerCraft's monitor_touch events have been known to be glitchy in SMP.

Truth be told, since CC 1.6 they occasionally mess up for me in SSP, too.

Not much you can do about it.
queebles #10
Posted 15 September 2014 - 06:44 AM
Well shoot, that bug is over a year old, guess I'll just get rid of the monitor and use mouse clicks on the terminal.