Page 1 of 1
how to print a full table with shortcut code on one line?
Posted: Mon Jan 07, 2013 8:22 pm
by Komag
This is an odd question, I know, but I've been trying to figure it out for a few hours and I'm just not a coder so I need help (and it may not be possible).
If I have this table:
Code: Select all
t = {"H","a","v","e"," ","a"," ","n","i","c","e"," ","d","a","y","." }
I can print it all together on one line like this:
Code: Select all
print(t[1]..t[2]..t[3]..t[4]..t[5]..t[6]..t[7]..t[8]..t[9]..t[10]..t[11]..t[12]..t[13]..t[14]..t[15]..t[16])
my question is, how to do that without typing all that crap out? something like:
but of course that doesn't work at all, just gives "unexpected symbol near 'for'" error message
any ideas?
Re: how to print a full table with shortcut code on one line
Posted: Mon Jan 07, 2013 8:31 pm
by Diarmuid
There you go, wrote you up a quick function:
Code: Select all
test = {"H","a","v","e"," ","a"," ","n","i","c","e"," ","d","a","y","." }
function concatenateTable(t)
local tString = ""
for i = 1, #t do
if t[i] then
tString = tString..t[i]
end
end
return tString
end
print(concatenateTable(test))
Re: how to print a full table with shortcut code on one line
Posted: Mon Jan 07, 2013 8:48 pm
by JKos
table.concat({"a", "b", "c"}, ",") (separated by ,)
or table.concat({"a", "b", "c"}) no delimeters
Re: how to print a full table with shortcut code on one line
Posted: Mon Jan 07, 2013 8:54 pm
by Komag
holy crap Diarmuid, I understand it, thanks! So you just "build up the string" - I found some comments while googling to that effect, but I didn't know how to approach handing the string.
Brilliant stuff, and so elegant and simple, thanks again.
-------
and JKos, thanks for that bit too, I can use it when I want to print the full table and not just a portion - my example showed the full table but my application will mostly be for partial tables. I may end up building separate unique tables on the fly and can then just use that snippet to print them
Re: how to print a full table with shortcut code on one line
Posted: Mon Jan 07, 2013 9:05 pm
by JKos
You can join a portion of a table with table.concat
table.concat({"a", "b", "c"},"",1,2) returns "ab"
see:
http://lua-users.org/wiki/TableLibraryTutorial
Re: how to print a full table with shortcut code on one line
Posted: Mon Jan 07, 2013 9:08 pm
by Komag
Hmm, that might be cleaner then. Maybe if I need to add other parts to a string first I can do some combination, but otherwise that does seem more direct
That's a nice resource link too, thanks
Re: how to print a full table with shortcut code on one line
Posted: Mon Jan 07, 2013 9:12 pm
by Diarmuid
Ah great, I rewrote some existing lua functionnality... well, it was a quick little problem to solve
. Thanks jKos!
Re: how to print a full table with shortcut code on one line
Posted: Mon Jan 07, 2013 11:26 pm
by Komag
Okay, this is deeper than I thought, and I need more help!
I'm hoping to be able to use the table.concat to output a string but then I need to have that string get processed as code (because it's nested) to output the real string!
So my test example is this:
Code: Select all
function testPrint()
local t = {"H","a","v","e"," ","a"," ","n","i","c","e"," ","d","a","y","." }
local u = {}
for i=1,#t-1 do table.insert(u,"l()..") end
table.insert(u,"l()")
print(table.concat(u))
print(l()..l()..l()..l()..l()..l()..l()..l()..l()..l()..l()..l()..l()..l()..l()..l())
end
function l()
local letters = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" }
return letters[math.random(1,#letters)]
end
What I want is for both print commands to print out a set of 16 random letters. Right now only the second print command does that; the first print command just prints out literally "l()..l()..l()..etc" as a string itself instead of processing that code to the final string.
I tried
but that's just the same thing.
So how can I get the code inside the print() to process TWICE before printing the resulting string?
Re: how to print a full table with shortcut code on one line
Posted: Mon Jan 07, 2013 11:36 pm
by Komag
wait a minute, I think I can do it with the first answer you gave Diarmuid, by "building up the string", hmm...
EDIT - crap, no, that doesn't work, and I should have known better, because it's the same problem, it's already a "string" so it doesn't get processed anymore, just printed
Re: how to print a full table with shortcut code on one line
Posted: Mon Jan 07, 2013 11:53 pm
by Komag
I found a roundabout way that is not quite the approach I wanted:
Code: Select all
function testPrint()
local t = {"H","a","v","e"," ","a"," ","n","i","c","e"," ","d","a","y","." }
local u = {}
for i=1,#t do table.insert(u,l()) end
print(table.concat(u))
print(l()..l()..l()..l()..l()..l()..l()..l()..l()..l()..l()..l()..l()..l()..l()..l())
end
function l()
local letters = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" }
return letters[math.random(1,#letters)]
end
This now works, because in building the table u it processed the l() for each piece. It's a different order of operations than I planned for the real usage later on, but it might work out