This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
kaj's profile picture

How do I (can I) print a variable in a multi-line String ?

Started by kaj, 21 September 2012 - 02:08 AM
kaj #1
Posted 21 September 2012 - 04:08 AM
Hello, pros of CC forums
First let me say I'm very new to Lua.

I recently learnt how to do multi - line strings - very handy things

print[[
multi
string
goodness
]]
And I wondered, Is it possible for me to include variables in said strings -
so I can do stuff like (for example - I know this is wrong code)

x,y,z = 1,2,2
print[[
varX = x
varY = y
varZ = z
]]
and have it print this?

varX = 1
varY = 2
varZ = 3

In sort of the same way you would do:

x=1
print('varX= '..x)
or

y=2
s = 'varY= '..y
print(s)
?

thanks for your time.

kaj
Luanub #2
Posted 21 September 2012 - 04:12 AM
Since the [[ ]] takes whats inside it literally, I don't think you can use vars.
Grim Reaper #3
Posted 21 September 2012 - 04:14 AM
You would use the exact same method in multi-line strings:

var1 = 1
var2 = 2
var3 = 3
print(
[[
var1 = ]] .. var1 .. [[

var2 = ]] .. var2 .. [[

var3 = ]] .. var3 .. [[
]]
)

Hope I helped! :)/>/>
kaj #4
Posted 21 September 2012 - 05:06 AM
That was fast… I like these forums.

I see what you did there….
so you close the brackets every time you use a variable..
At first glance it looks almost the same as not doing it in a multi-line, but more sleek :)/>/>.

thanks very much. +1
ryanv09 #5
Posted 21 September 2012 - 05:35 AM
If you are familiar with printf from other languages, you could do something like this:

print(string.format("Text text %d text", some_integer)) 

whatever the value of some_integer is will replace %d, just like in printf. Put in n in the string for new lines.
cant_delete_account #6
Posted 21 September 2012 - 06:00 AM
If you are familiar with printf from other languages, you could do something like this:

print(string.format("Text text %d text", some_integer)) 

whatever the value of some_integer is will replace %d, just like in printf. Put in n in the string for new lines.
This is not relevant to the question he asked. He asked 'how can I print a variable in a multi-line string', not 'how can I print a variable in a string'.
Lyqyd #7
Posted 21 September 2012 - 05:51 PM
If you are familiar with printf from other languages, you could do something like this:

print(string.format("Text text %d text", some_integer)) 

whatever the value of some_integer is will replace %d, just like in printf. Put in n in the string for new lines.
This is not relevant to the question he asked. He asked 'how can I print a variable in a multi-line string', not 'how can I print a variable in a string'.

No, it is relevant to the question. It does not show the string in multiple lines in the code, but the escaped sequence n should create a newline in the output.