PDA

View Full Version : What is the syntax for string capturing into a table?



Minty23185Fresh
January 20th, 2019, 17:32
Is there a way to capture the results of string.match() into a table instead of a series of variables?

Instead of this:


local var1, var2, var3 = string.match(sData, sCapture);

yielding, say, this


var1 = "Fred", var2 = 3.14159, var3 = "<h>Aliens</h>"


Something like this:


local myTable = { };
myTable = totable(string.match(sData, sCapture));

which yields


myTable = { "Fred", 3.14159, "<h>Aliens</h>" }


I realize "totable()" doesn't exist and that the syntax is goofy, but I'm trying to illustrate my point as best that I can

Moon Wizard
January 23rd, 2019, 00:07
The return value of string.match is determined by Lua; and there is no such function as totable(). So, anything you do needs to be coded yourself.

You could probably do something like this:


function totable(...)
local aTable = {};

for i = 1, select("#", ...) do
local v = select(i, ...);
table.insert(aTable, v);
end

return aTable;
end


Note, I haven't tested this code, just off the top of my head; so it might need some fixing up.

Regards,
JPG

Andraax
January 23rd, 2019, 00:15
Is there a way to capture the results of string.match() into a table instead of a series of variables?

Why don't you just do:


local myTable = { string.match(sData, sCapture) };

Minty23185Fresh
January 23rd, 2019, 17:17
You could probably do something like this...
Thanks Moon Wizard I ended up with something similar...



Why don't you just do: local myTable = { string.match(sData, sCapture) };
Thanks Andraax. Precisely what I needed.