how to print a full table with shortcut code on one line?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

how to print a full table with shortcut code on one line?

Post 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:

Code: Select all

print(for i=1,16 do t[i].. end)
but of course that doesn't work at all, just gives "unexpected symbol near 'for'" error message

any ideas?
Finished Dungeons - complete mods to play
User avatar
Diarmuid
Posts: 807
Joined: Thu Nov 22, 2012 6:59 am
Location: Montreal, Canada
Contact:

Re: how to print a full table with shortcut code on one line

Post 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))
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: how to print a full table with shortcut code on one line

Post by JKos »

table.concat({"a", "b", "c"}, ",") (separated by ,)

or table.concat({"a", "b", "c"}) no delimeters
;)
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: how to print a full table with shortcut code on one line

Post 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
Finished Dungeons - complete mods to play
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: how to print a full table with shortcut code on one line

Post 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
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: how to print a full table with shortcut code on one line

Post 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
Finished Dungeons - complete mods to play
User avatar
Diarmuid
Posts: 807
Joined: Thu Nov 22, 2012 6:59 am
Location: Montreal, Canada
Contact:

Re: how to print a full table with shortcut code on one line

Post by Diarmuid »

Ah great, I rewrote some existing lua functionnality... well, it was a quick little problem to solve :). Thanks jKos!
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: how to print a full table with shortcut code on one line

Post 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

Code: Select all

c = table.concat(u)
print(c)
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?
Finished Dungeons - complete mods to play
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: how to print a full table with shortcut code on one line

Post 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
Finished Dungeons - complete mods to play
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: how to print a full table with shortcut code on one line

Post 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
Finished Dungeons - complete mods to play
Post Reply