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

What is "..." With Tables/Functions?

Started by OrangeC7, 27 February 2018 - 04:47 PM
OrangeC7 #1
Posted 27 February 2018 - 05:47 PM
I've been trying to find information on exactly WHAT the "…" input does, and I couldn't find anything about it.
(I tried searching it on the forums, but "…" is counted as less than 4 chars. ¯\_(ツ)_/¯)
Anyways, the thing I know about this 'operator'(?) is that it is used for getting the user's input on arguments passed with a program through the shell, like the following:

local tArgs = {...}
local function allArgs()
  for i=1,#tArgs do
	local temp = ""
	if tArgs[i] ~= nil then
	  temp = temp..tostring(tArgs[i])
	end
  end
  return temp
end
print("You executed this program with the arguments "..allArgs())
but I don't know if it's just something as a part of computercraft or something useful I can actually use, because I noticed that the "…" thing was used in the local shell function "tokenize()":

function tokenise( ... )
  local sLine, tWords, bQuoted = table.concat( { ... }, " " ), {}, false
  for match in string.gmatch( sLine .. "\"", "(.-)\"" ) do
	if bQuoted then
	  table.insert( tWords, match )
	else
	  for m in string.gmatch( match, "[^ \t]+" ) do
		table.insert( tWords, m )
	  end
	end
	bQuoted = not bQuoted
  end
  return tWords
end
So, can someone explain this please?
Edited on 27 February 2018 - 04:48 PM
Luca_S #2
Posted 27 February 2018 - 05:55 PM
The "…" operator is used to indicate Varargs

If you want a function that adds multiple values you could do it like that:

function add(...)
  local params={...} --This wraps the varargs in a table
  local res
  for i = 1,#params do
	res = res + params[i]
  end
  return res
end
print(add(1)) --1
print(add(1,2)) --3
print(add(5,1,3,1)) --10

Varargs are also used by the shell to give arguments to a program, which is why most programs wrap them in a table like this:

local tArgs = {...}
Edited on 27 February 2018 - 04:56 PM
OrangeC7 #3
Posted 27 February 2018 - 06:24 PM
The "…" operator is used to indicate Varargs

If you want a function that adds multiple values you could do it like that:

function add(...)
  local params={...} --This wraps the varargs in a table
  local res
  for i = 1,#params do
	res = res + params[i]
  end
  return res
end
print(add(1)) --1
print(add(1,2)) --3
print(add(5,1,3,1)) --10
I see… Thanks!