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

atting qotes to a returned string without quotes

Started by kain184, 25 January 2015 - 05:43 PM
kain184 #1
Posted 25 January 2015 - 06:43 PM
hello. i am running some code that requests the raw name of an item. when the raw name is returned it is given as a string as such item.name but i need to be able to read it as a string "item.name" is there any way to add quotes to a strings return value that is given automaticly
KingofGamesYami #2
Posted 25 January 2015 - 06:44 PM

local function inQuotes( text )
  return '"' .. text .. '"'
end
kain184 #3
Posted 25 January 2015 - 06:52 PM
i was thinking something along the same lines i just could not figure out how to get it to treat the quotes as not a string item. thank you very much
Dragon53535 #4
Posted 26 January 2015 - 01:58 PM
To explain further. Both ' and " are valid string identifiers, however when one is used to start a string, the other is "ignored" and when used, will not terminate the string.
Possseidon #5
Posted 26 January 2015 - 08:23 PM
instead of mixing "text" and 'text' (and [[text]] (yes that also is a string xD)) you can also do something like this:

myString = "\"a string surounded by quotes\""

\" will prevent lua from thinking the " is marking the end of the last string and just adds in an actual " to it :P/>
kain184 #6
Posted 27 January 2015 - 03:45 AM
both great awnsers used the first one perfictly have another minor question as well been snooping around the tutorials and forums but cant seem to find a difinitive awnser is it possable to do something like

function name1()
screen=1
end
function name2()
screen=2
end
------------------------------
screen=1
while true do
"name" ..screen .."()"
end
im using a button control that wil set it after a get click action for the value of screen
Bomb Bloke #7
Posted 27 January 2015 - 04:04 AM
If I wanted to do something along those lines, I'd use a table:

local screen = 1

local name = {
	function()
		screen = 2
	end,
	
	function()
		screen = 1
	end}

while true do
	name[screen]()
end

… though I suspect you'd be better off just doing:

local screen = 1

local function name()
	if screen == 1 then
		screen = 2
	else
		screen = 1
	end
end

while true do
	name()
end
kain184 #8
Posted 27 January 2015 - 04:06 AM
i think the first will do my purposes better becouse i want it to change info screens while my program is running after the inital click so it will be something that is repeated alot so i didnt want alot of if statements in the mix.