Jump to content




How to unpack a table into a string?

api lua help

  • You cannot reply to this topic
2 replies to this topic

#1 Dahknee

  • Members
  • 1,808 posts
  • Location/home/da

Posted 27 August 2014 - 01:25 AM

Okay so I am trying to recreate the read program, adding my own bits and pieces...

The thing I am stuck on is outputting the table as a string without all the syntax... I found a function in Lua called unpack() which does exactly what I want... but it doesn't seem to be working... are there any other ways?

Here is my code anyway:
function read(sAlt, sStart)
  local tReadData = {}
  local nPos = 0
  xPos, yPos = term.getCursorPos()
  term.setCursorPos(1, yPos + 1)
  while true do
	local args = { os.pullEvent() }
	if sStart then
	  nStart = string.len(sStart)
	  write(sStart)
	end
	if args[1] == "key" then
	  if args[2] == 28 then
		return unpack(tReadData)
	  elseif args[2] == 14 then
		-- add back key use table.remove()
		-- and clear the char space.
	  end
	elseif args[1] == "char" then
	  table.insert(tReadData, args[2])
	  nPos = nPos + 1
	  if sAlt then
		write(sAlt)
	  else
		write(args[2])
	  end
	end
  end
end

print("-----------------------")
local text = read()
term.setCursorPos(1,10)
print(text)

Forgot to say it doesn't work because when I print the returned variable it only prints the first entry in the table?

Edited by DannySMc, 27 August 2014 - 01:27 AM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 27 August 2014 - 01:28 AM

table.concat( tbl, seperation )

eg.
local tbl = { "hello", "goodby" }
local sTbl = table.concat( tbl, " " )
print( sTbl ) --#prints: "hello goodby"

Edited by KingofGamesYami, 27 August 2014 - 01:29 AM.


#3 natedogith1

  • Members
  • 110 posts

Posted 27 August 2014 - 03:41 AM

You're returning all the values you seem to want, but you're storing them to a single variable. That variable is only storing the first argument returned, and as such you're only printing the first table entry.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users