Adding Tuples To a Table

As the topic name suggests.
Right now, i’m using string.find() and trying to store the start and end values of a string.
Is there a better way I can do this other than this?:

	local Table = { 
		TupleTable = {}
	}

	local Start, End = string.find(String, "find")
	Table.TupleTable = {Start = Start, End = End}
1 Like

question unclear. string.find doesn’t store anything

You can use table.unpack on an array and make it act like a tuple:

local tab = {}

tab.tupleTable = {string.find(str, pattern)}
local start, finish = table.unpack(tab.tupleTable)

print(table.unpack(tupleTable)) --> prints start, finish

Although, why would you need to store tuples anyway?

1 Like

You could do something like:

Table.TupleTable = table.pack(string.find(String, "find"))

However that would give you the results in an array format, so you’d have to retrieve them with numbers 1 and 2 rather than Start and End.

1 Like

I didn’t know about either table.pack or table.unpack, thank you to both of you.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.