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

Having problem with string in function argument

Started by Marcono1234, 26 September 2014 - 03:43 PM
Marcono1234 #1
Posted 26 September 2014 - 05:43 PM
I have a little problem with a programm I want to make, I have the function logAction(runningFunction, action, actionType)
And I have a line of code in a function called getTo(x,y,z,dir,mine):

logAction("getTo()",[["Reached successfully position" ..x.." "..y.." "..z..]],0)
And I read here that for complicated strings it is better to use the [[ and ]], but this seems not to work somehow, it just prints then:
"Reached successfully position" ..x.." "..y.." "..z..

Does anyone has a clue how to fix this?
Already thank you for your help :)/>
MKlegoman357 #2
Posted 26 September 2014 - 07:03 PM
There are four main ways of defining a string in Lua:

A. Using double quotes:

local myString = "This is a string"

B. Using single quotes:

local myString = 'This is a string'

C. Using two square brackets:

local myString = [[This is a string]]

D. Using two square brackets and an equal sign in between:

local myString = [=[This is a string]=]

Now, A and B are pretty much the same. C and D are pretty much the same too, but they are different than A and B. A and B are called single-line strings and C and D are called multi-line strings. When you define a string using A or B, the string definition must be in one line and all special characters must be escaped (ex.: \n, \", \', etc..). When you define a string using C or D, you don't have to escape some special characters, like " or '. Also, multiline can span multiple lines and all the line breaks in the multi-line string are also converted to \n.

To put it in an example, here is the same string defined with all these four methods:


s = "This is a \" string ' with some special characters and \n new lines"

s = 'This is a " string \' with some special characters and \n new lines'

s = [[This is a " string ' with some special characters and 
 new lines]]

s = [=[This is a " string ' with some special characters and 
 new lines]=]

I hope you can see your problem now :)/>
Edited on 26 September 2014 - 05:05 PM
Marcono1234 #3
Posted 26 September 2014 - 07:32 PM
There are four main ways of defining a string in Lua:

A. Using double quotes:

local myString = "This is a string"

B. Using single quotes:

local myString = 'This is a string'

C. Using two square brackets:

local myString = [[This is a string]]

D. Using two square brackets and an equal sign in between:

local myString = [=[This is a string]=]

Now, A and B are pretty much the same. C and D are pretty much the same too, but they are different than A and B. A and B are called single-line strings and C and D are called multi-line strings. When you define a string using A or B, the string definition must be in one line and all special characters must be escaped (ex.: \n, \", \', etc..). When you define a string using C or D, you don't have to escape some special characters, like " or '. Also, multiline can span multiple lines and all the line breaks in the multi-line string are also converted to \n.

To put it in an example, here is the same string defined with all these four methods:


s = "This is a \" string ' with some special characters and \n new lines"

s = 'This is a " string \' with some special characters and \n new lines'

s = [[This is a " string ' with some special characters and
new lines]]

s = [=[This is a " string ' with some special characters and
new lines]=]

I hope you can see your problem now :)/>
A little bit maybe, thank you, but I can't solve my problem with this…
leaving the "s away and using [[]] doesn't work and I have no idea how to solve this, would you be se kind and also transform my line :)/> ?
MKlegoman357 #4
Posted 26 September 2014 - 08:06 PM
Just remove the '[[' and ']]' from your string.
Marcono1234 #5
Posted 26 September 2014 - 09:53 PM
Just remove the '[[' and ']]' from your string.
I am sorry, but this isn't working, when I use

logAction("getTo()",("Reached successfully position" ..x.." "..y.." "..z..),0)
It says
unexpected symbol
KingofGamesYami #6
Posted 26 September 2014 - 10:03 PM
please post the rest of the code, that line would not cause that error unless it was mistyped.

edit: Nevermind, it's right there… remove ".." after z.

Eg.

logAction("getTo()",("Reached successfully position" ..x.." "..y.." "..z),0)
Edited on 26 September 2014 - 08:55 PM
Lyqyd #7
Posted 26 September 2014 - 10:22 PM
The fourth type listed above isn't a separate type. Block quotes are one open square bracket, n equals signs, and a second open square bracket, ended by the same character combination except with closing square braces, where n is any number zero or larger. I don't believe there is an upper limit.
Marcono1234 #8
Posted 27 September 2014 - 01:45 PM
Thank you
KingofGamesYami

now it is working :)/>
MKlegoman357 #9
Posted 27 September 2014 - 02:36 PM
The fourth type listed above isn't a separate type. Block quotes are one open square bracket, n equals signs, and a second open square bracket, ended by the same character combination except with closing square braces, where n is any number zero or larger. I don't believe there is an upper limit.

Ah, never thought about it like that. Thanks for pointing it out :)/>