Possible to Create Unkown Number of Variables Assigned to Something?

  1. What do you want to achieve? I want to set an unknown number of variables to an unknown amount of values. I am in need of this because using the ‘unpack’ function it only returns what variables you have set it to.

  2. What is the issue? I would like to use the string.find function on unpacking a table but I am facing some problems. It works if you concatenate all the variables together and then do string.find, but not otherwise.

local s1, s2, s3 = unpack({"first","second","third"})
print(string.find(s1..s2..s3,"second")) -- prints 6 11

I need to have this work for every amount of possible unpack returns. Something like

nthvariables = unpack({"hi","middle","bye"})
print(string.find(nthvariables,"hi"))

Where nthvariables could be 0, 1, or even 1,000 different unpacked values.

  1. What solutions have you tried so far? I tried doing this and other approaches but nothing seemed to work. I also tried doing a recursive function but again I ran into the problem of only seeing the first value of unpack when I passed it to a … in the function.
local function varset(C_String,Iteration,...)
	local args = ...
	local cumulativestring = C_String
	cumulativestring = cumulativestring .. args[Iteration]
	if Iteration < #args then
		return varset(cumulativestring,Iteration+1,args)
	else 
		return cumulativestring
	end
end

This would have worked but … only resulted in the first unpacked value.

You have to concatenate ... as a table to index from it

local args = {...}

Would you then have to do args[1][1]? The problem here was not really the … it was the unpacking part. I could perfectly well see the … indices.

You can use table.concat to concatenate an array of strings.

local t = {"first", "secod", "third"}
local s = table.concat(t, "")
print(string.find(s, "second"))

Thanks, will look into this soon. One question though, what is the second argument to table.concat, is it the space between?

Its a string to put between each element, for example table.concat(t, ", ")
would result in first, second, third

1 Like

Thanks! It solved the problem!