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

I need to bring 19 strings down to 1

Started by TheJogMan, 21 April 2014 - 06:18 PM
TheJogMan #1
Posted 21 April 2014 - 08:18 PM
What I'm doing is I'm trying to make an on screen keyboard that works through the mouse_click or the monitor_touch events and the way I have it now is each character has it's own variable but I need to combine all the separate strings into a single string t be used in the rest of the script.

The code that handles the keyboard itself is a separate script and I intend to get the input to other scripts through the fs api

The rest of it works fine and if possible I would lie to avoid using tables or for loops like I've seen in other solutions to this issue because I'm still some what new to Lua and don't quit understand how they work yet and I would feel a lot more comfortable not having to deal with stuff I don't understand yet but I don't mind If it's unavoidable.



W, H = term.getSize()
WM = W / 2
HM = H / 2
b = paintutils.loadImage('board')
char = " "
char1 = char
char2 = char
char3 = char
char4 = char
char5 = char
char6 = char
char7 = char
char8 = char
char9 = char
char10 = char
char11 = char
char12 = char
char13 = char
char14 = char
char15 = char
char16 = char
char17 = char
char18 = char
char19 = char
char1u = 0
char2u = 0
char3u = 0
char4u = 0
char5u = 0
char6u = 0
char7u = 0
char8u = 0
char9u = 0
char10u = 0
char11u = 0
char12u = 0
char13u = 0
char14u = 0
char15u = 0
char16u = 0
char17u = 0
char18u = 0
char19u = 0
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setCursorPos(WM - 9,HM - 1)
print("Q W E R T Y U I O P")
term.setCursorPos(WM - 9,HM + 1)
print("A S D F G H J K ENT")
term.setCursorPos(WM - 9,HM + 3)
print("Z X C V B N M L SPA")
paintutils.drawImage(b,WM - 10,HM - 4)
ent = 0
repeat
	 paintutils.drawLine(WM - 9,HM - 3,WM + 9,HM - 3,colors.white)
	 term.setCursorPos(WM - 9,HM - 3)
	 print(char1)
	 term.setCursorPos(WM - 8,HM - 3)
	 print(char2)
	 term.setCursorPos(WM - 7,HM - 3)
	 print(char3)
	 term.setCursorPos(WM - 6,HM - 3)
	 print(char4)
	 term.setCursorPos(WM - 5,HM - 3)
	 print(char5)
	 term.setCursorPos(WM - 4,HM - 3)
	 print(char6)
	 term.setCursorPos(WM - 3,HM - 3)
	 print(char7)
	 term.setCursorPos(WM - 2,HM - 3)
	 print(char8)
	 term.setCursorPos(WM - 1,HM - 3)
	 print(char9)
	 term.setCursorPos(WM,HM - 3)
	 print(char10)
	 term.setCursorPos(WM + 1,HM - 3)
	 print(char11)
	 term.setCursorPos(WM + 2,HM - 3)
	 print(char12)
	 term.setCursorPos(WM + 3,HM - 3)
	 print(char13)
	 term.setCursorPos(WM + 4,HM - 3)
	 print(char14)
	 term.setCursorPos(WM + 5,HM - 3)
	 print(char15)
	 term.setCursorPos(WM + 6,HM - 3)
	 print(char16)
	 term.setCursorPos(WM + 7,HM - 3)
	 print(char17)
	 term.setCursorPos(WM + 8,HM - 3)
	 print(char18)
	 term.setCursorPos(WM + 9,HM - 3)
	 print(char19)
	 eve, buto, x, y = os.pullEvent()
	 if eve == "mouse_click" then
		  if x == 16 and y == 8 then
			   char = "Q"
			  elseif x == 18 and y == 8 then
			   char = "W"
			  elseif x == 20 and y == 8 then
			   char = "E"
			  elseif x == 22 and y == 8 then
			   char = "R"
			  elseif x == 24 and y == 8 then
			   char = "T"
			  elseif x == 26 and y == 8 then
			   char = "Y"
			  elseif x == 28 and y == 8 then
			   char = "U"
			  elseif x == 30 and y == 8 then
			   char = "I"
			  elseif x == 32 and y == 8 then
			   char = "O"
			  elseif x == 34 and y == 8 then
			   char = "P"
			  elseif x == 16 and y == 10 then
			   char = "A"
			  elseif x == 18 and y == 10 then
			   char = "S"
			  elseif x == 20 and y == 10 then
			   char = "D"
			  elseif x == 22 and y == 10 then
			   char = "F"
			  elseif x == 24 and y == 10 then
			   char = "G"
			  elseif x == 26 and y == 10 then
			   char = "H"
			  elseif x == 28 and y == 10 then
			   char = "J"
			  elseif x == 30 and y == 10 then
			   char = "K"
			  elseif x == 16 and y == 12 then
			   char = "Z"
			  elseif x == 18 and y == 12 then
			   char = "X"
			  elseif x == 20 and y == 12 then
			   char = "C"
			  elseif x == 22 and y == 12 then
			   char = "V"
			  elseif x == 24 and y == 12 then
			   char = "B"
			  elseif x == 26 and y == 12 then
			   char = "N"
			  elseif x == 28 and y == 12 then
			   char = "M"
			  elseif x == 30 and y == 12 then
			   char = "L"
			  elseif x > 31 and x < 35 and y == 10 then
			   char = "ENT"
			  elseif x > 31 and x < 35 and y == 12 then
			   char = " "
			  end
		 end
	 if eve == "mouse_click" then
		  if char == "ENT" then
			   ent = 1
			  elseif char1u == 0 then
			   char1 = char
			   char1u = 1
			  else
			   if char2u == 0 then
					char2 = char
					char2u = 1
				   else
					if char3u == 0 then
						 char3 = char
						 char3u = 1
						else
						 if char4u == 0 then
							  char4 = char
							  char4u = 1
							 else
							  if char5u == 0 then
								   char5 = char
								   char5u = 1
								  else
								   if char6u == 0 then
										char6 = char
										char6u = 1
									   else
										if char7u == 0 then
											 char7 = char
											 char7u = 1
											else
											 if char8u == 0 then
												  char8 = char
												  char8u = 1
												 else
												  if char9u == 0 then
													   char9 = char
													   char9u = 1
													  else
													   if char10u == 0 then
														   char10 = char
														   char10u = 1
														  else
														   if char11u == 0 then
																char11 = char
																char11u = 1
															   else
																if char12u == 0 then
																	 char12 = char
																	 char12u = 1
																	else
																	 if char13u == 0 then
																		  char13 = char
																		  char13u = 1
																		 else
																		  if char14u == 0 then
																			   char14 = char
																			   char14u = 1
																			  else
																			   if char15u == 0 then
																					char15 = char
																					char15u = 1
																				   else
																					if char16u == 0 then
																						 char16 = char
																						 char16u = 1
																						else
																						 if char17u == 0 then
																							  char17 = char
																							  char17u = 1
																							 else
																							  if char18u == 0 then
																								   char18 = char
																								   char18u = 1
																								  else
																								   if char19u == 0 then
																										char19 = char
																										char19u = 1
																									   end
																								  end
																							 end
																						end
																				   end
																			  end
																		 end
																	end
															   end
														  end
													 end
												end
										   end
									  end
								 end
							end
					   end
				  end
			 end
		end
	until ent == 1

And also I'm only asking for help on this issue I'm not asking for opinions on how mess this is

But if you do take the time to re do it all in a neater format I wont complain

I suggest running the code yourself to try and get a better understanding on how it works but the goal is to combine the variables:

char1 char2 char3 char4 char5 char6 char7 char8 char9 char10 char11 char12 char13 char14 char15 char16 char17 char18 char19

into 1 single string


Sorry if I was supposed to provide the script in a different way I had a feeling I was doing something wrong when it wouldn't let me paste he whole thing at once and it does have proper indentation in the actual script (figured out how to provide it correctly :)/>)
Edited on 21 April 2014 - 06:41 PM
electrodude512 #2
Posted 21 April 2014 - 08:58 PM
It would really be much, much easier to learn how to use tables and loops than have to use that monster you wrote. They aren't as scary as they sound - you're actually already using 3 tables (_G, term, and paintutils), although you probably don't realize that.

DO NOT COMBINE THEM INTO A STRING!!!!!! That would be reinventing the wheel (or, in this case, the table). You will begin to have suicidal thoughts if you do that when you see all the string.sub's and the string.match's. You will feel like you're programming a TI-84, which is not a good feeling to have. CC uses one of the most fun programming languages thanks to Lua's tables. Doing strings by stuffing them in one big string is NOT FUN. Use a table, it's so much easier.

Tables are easy:

local chars = {}   --this makes a table called chars, makes it local too so we don't interfere with the global table (you don't need local but it's better)
chars[1] = "a"	--put stuff in the table
chars[2] = "b"
chars["pi"] = math.pi   --oh wait, pi isn't a character...

print(chars[1])	  --prints "a"
print(chars.pi)	  --prints 3.1415926535
print(math.pi)	   --math is a table, just like chars!  So are string, table, paintutils, term, shell, etc.  This prints 3.1415926535, just like the previous line.

So is are loops:

local chars = {}
for i=32,127 do   --make i=32, then do the stuff inside the for loop, then make i=33, then do the stuff in the for loop, then make i=34, then do the stuff in the for loop, ... then make i=127, then do the stuff in the for loop, then stop looping
	chars[i] = string.char(i)		 --string.char converts number to ascii, so 32=" ", 65="A", etc.; get the character and put it in the table chars
end

--now print the characters in reverse order
local i=127
while i>=32 do  --do the stuff in the loop while i is greater than or equal to 32
   print(chars[i])	 --get character i out of the table and print it
   i=i-1
end
See if that helps you fix your code. Also see the lua manual.
Edit: You said you don't want an opinion on what a mess your code is. I don't have a problem with messy code - I write tons of it. However, if I didn't use tables or loops I would have never been able to write any of the programs I've written. If you don't use any tables or loops your code will reach near infinite complexity and you will give up because your code will just be too messy for even you to use. Also, what you can do without loops is extremely limited - you would have a hard time making a door lock without any (but unfortunately this has been done).
Edited on 21 April 2014 - 07:09 PM
TheJogMan #3
Posted 22 April 2014 - 12:46 AM
Thanks so much I've tried to figure out how to use tables before but I don't know if I'm coming in with the wrong mind set but they've never made any sense and if they did it was still too complicated for me to remember long enough but your explanation makes a lot more sense to me thanks

This is the final product it y see minor adjustments like lower case letters and numbers but it works exactly as intended in the main script I just have fs assign the table to the value of a variable and it works perfectly.

electrodude512 you will definitely be given credit for the final product that this will be used in called jogOS so keep an eye out for it because its looking pretty good (well compared to what I've done in the past anyway)


W, H = term.getSize()
WM = W / 2
HM = H / 2
b = paintutils.loadImage('board')
char = " "
char1 = char
char2 = char
char3 = char
char4 = char
char5 = char
char6 = char
char7 = char
char8 = char
char9 = char
char10 = char
char11 = char
char12 = char
char13 = char
char14 = char
char15 = char
char16 = char
char17 = char
char18 = char
char19 = char
char1u = 0
char2u = 0
char3u = 0
char4u = 0
char5u = 0
char6u = 0
char7u = 0
char8u = 0
char9u = 0
char10u = 0
char11u = 0
char12u = 0
char13u = 0
char14u = 0
char15u = 0
char16u = 0
char17u = 0
char18u = 0
char19u = 0
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setCursorPos(WM - 9,HM - 1)
print("Q W E R T Y U I O P")
term.setCursorPos(WM - 9,HM + 1)
print("A S D F G H J K ENT")
term.setCursorPos(WM - 9,HM + 3)
print("Z X C V B N M L SPA")
paintutils.drawImage(b,WM - 10,HM - 4)
ent = 0
repeat
	 paintutils.drawLine(WM - 9,HM - 3,WM + 9,HM - 3,colors.white)
	 term.setCursorPos(WM - 9,HM - 3)
	 print(char1)
	 term.setCursorPos(WM - 8,HM - 3)
	 print(char2)
	 term.setCursorPos(WM - 7,HM - 3)
	 print(char3)
	 term.setCursorPos(WM - 6,HM - 3)
	 print(char4)
	 term.setCursorPos(WM - 5,HM - 3)
	 print(char5)
	 term.setCursorPos(WM - 4,HM - 3)
	 print(char6)
	 term.setCursorPos(WM - 3,HM - 3)
	 print(char7)
	 term.setCursorPos(WM - 2,HM - 3)
	 print(char8)
	 term.setCursorPos(WM - 1,HM - 3)
	 print(char9)
	 term.setCursorPos(WM,HM - 3)
	 print(char10)
	 term.setCursorPos(WM + 1,HM - 3)
	 print(char11)
	 term.setCursorPos(WM + 2,HM - 3)
	 print(char12)
	 term.setCursorPos(WM + 3,HM - 3)
	 print(char13)
	 term.setCursorPos(WM + 4,HM - 3)
	 print(char14)
	 term.setCursorPos(WM + 5,HM - 3)
	 print(char15)
	 term.setCursorPos(WM + 6,HM - 3)
	 print(char16)
	 term.setCursorPos(WM + 7,HM - 3)
	 print(char17)
	 term.setCursorPos(WM + 8,HM - 3)
	 print(char18)
	 term.setCursorPos(WM + 9,HM - 3)
	 print(char19)
	 eve, buto, x, y = os.pullEvent()
	 if eve == "mouse_click" then
		  if x == 16 and y == 8 then
			   char = "Q"
			  elseif x == 18 and y == 8 then
			   char = "W"
			  elseif x == 20 and y == 8 then
			   char = "E"
			  elseif x == 22 and y == 8 then
			   char = "R"
			  elseif x == 24 and y == 8 then
			   char = "T"
			  elseif x == 26 and y == 8 then
			   char = "Y"
			  elseif x == 28 and y == 8 then
			   char = "U"
			  elseif x == 30 and y == 8 then
			   char = "I"
			  elseif x == 32 and y == 8 then
			   char = "O"
			  elseif x == 34 and y == 8 then
			   char = "P"
			  elseif x == 16 and y == 10 then
			   char = "A"
			  elseif x == 18 and y == 10 then
			   char = "S"
			  elseif x == 20 and y == 10 then
			   char = "D"
			  elseif x == 22 and y == 10 then
			   char = "F"
			  elseif x == 24 and y == 10 then
			   char = "G"
			  elseif x == 26 and y == 10 then
			   char = "H"
			  elseif x == 28 and y == 10 then
			   char = "J"
			  elseif x == 30 and y == 10 then
			   char = "K"
			  elseif x == 16 and y == 12 then
			   char = "Z"
			  elseif x == 18 and y == 12 then
			   char = "X"
			  elseif x == 20 and y == 12 then
			   char = "C"
			  elseif x == 22 and y == 12 then
			   char = "V"
			  elseif x == 24 and y == 12 then
			   char = "B"
			  elseif x == 26 and y == 12 then
			   char = "N"
			  elseif x == 28 and y == 12 then
			   char = "M"
			  elseif x == 30 and y == 12 then
			   char = "L"
			  elseif x > 31 and x < 35 and y == 10 then
			   char = "ENT"
			  elseif x > 31 and x < 35 and y == 12 then
			   char = " "
			  end
		 end
	 if eve == "mouse_click" then
		  if char == "ENT" then
			   ent = 1
			  elseif char1u == 0 then
			   char1 = char
			   char1u = 1
			  else
			   if char2u == 0 then
					char2 = char
					char2u = 1
				   else
					if char3u == 0 then
						 char3 = char
						 char3u = 1
						else
						 if char4u == 0 then
							  char4 = char
							  char4u = 1
							 else
							  if char5u == 0 then
								   char5 = char
								   char5u = 1
								  else
								   if char6u == 0 then
										char6 = char
										char6u = 1
									   else
										if char7u == 0 then
											 char7 = char
											 char7u = 1
											else
											 if char8u == 0 then
												  char8 = char
												  char8u = 1
												 else
												  if char9u == 0 then
													   char9 = char
													   char9u = 1
													  else
													   if char10u == 0 then
														   char10 = char
														   char10u = 1
														  else
														   if char11u == 0 then
																char11 = char
																char11u = 1
															   else
																if char12u == 0 then
																	 char12 = char
																	 char12u = 1
																	else
																	 if char13u == 0 then
																		  char13 = char
																		  char13u = 1
																		 else
																		  if char14u == 0 then
																			   char14 = char
																			   char14u = 1
																			  else
																			   if char15u == 0 then
																					char15 = char
																					char15u = 1
																				   else
																					if char16u == 0 then
																						 char16 = char
																						 char16u = 1
																						else
																						 if char17u == 0 then
																							  char17 = char
																							  char17u = 1
																							 else
																							  if char18u == 0 then
																								   char18 = char
																								   char18u = 1
																								  else
																								   if char19u == 0 then
																										char19 = char
																										char19u = 1
																									   end
																								  end
																							 end
																						end
																				   end
																			  end
																		 end
																	end
															   end
														  end
													 end
												end
										   end
									  end
								 end
							end
					   end
				  end
			 end
		end
	until ent == 1
chars = {}
chars[1] = char1
chars[2] = char2
chars[3] = char3
chars[4] = char4
chars[5] = char5
chars[6] = char6
chars[7] = char7
chars[8] = char8
chars[9] = char9
chars[10] = char10
chars[11] = char11
chars[12] = char12
chars[13] = char13
chars[14] = char14
chars[15] = char15
chars[16] = char16
chars[17] = char17
chars[18] = char18
chars[19] = char19
file = fs.open('inp','w')
file:write(chars)
file:close()
Edited on 22 April 2014 - 04:11 PM
KingofGamesYami #4
Posted 22 April 2014 - 03:27 AM
I'm not sure if you will find this helpful or not, but I have created a keyboard program
Spoiler
local mon = peripheral.wrap("right")
local dmon = peripheral.wrap("monitor_4")

local input, shiftPressed = " ", false

local keyboard = {
 [true] = {  "!@#$%^&amp;*()  |delete|", " QWERTYUIOP", "  ASDFGHJKL  |enter|", "   ZXCVBNM   |shift|"},
 [false] = { "1234567890  |delete|", " qwertyuiop", "  asdfghjkl  |enter|", "   zxcvbnm   |shift|"}
}

dmon.setTextScale(3)
mon.setTextScale(4)
mon.setBackgroundColor(colors.blue)
mon.clear()

while true do
 for i = 1, 4 do
  mon.setCursorPos(1, i)
  mon.write(keyboard[shiftPressed][i])
 end

 local event, side, x, y = os.pullEvent('monitor_touch')

 if x <= 11 and y <= 4 then
  local char = string.sub(keyboard[shiftPressed][y], x, x)
  if char ~= ' ' then
   input = input..char
  end

 elseif y == 1 and x >=13 and x <= 20 then
  input = input:sub(1, #input - 1)

 elseif x >= 14 and x <=20
  if y == 3 then
   if input == "Pa55w0rd" then
    rednet.open("left")
    rednet.broadcast("success!")
    rednet.close("left")
   end
  elseif y == 4 then
   shiftPressed = not shiftPressed
  end
 end
 dmon.clear()
 dmon.setCursorPos(1, 2)
 dmon.write(input)
 sleep(0.01)
end
It only runs with a certain monitor configuration, and doesn't run on the terminal, but I think you may find it helpful.
Edited on 22 April 2014 - 01:28 AM