1

I have a question about string concatenation.

I have this example where I am trying to append a value inside a json block with a variable value

example:

clock = os.clock()
body = "{\"name\":\"stringValue\" .. clock }"
print(body) 

When I run this I get the following output:

{"name":"stringValue" .. clock }

What I am expecting is something like

{"name":"stringValue0.010117"}

How do I make is so this variables value is added to the string?

1
  • 1
    Try "{\"name\":\"stringValue\"" .. clock .. "}" instead. You have to end a string before the concatenation operator. Commented Apr 29, 2015 at 14:22

1 Answer 1

3

This is an instance were using [[ ]] delimited strings is useful:

clock = os.clock()
body = [[{"name":"stringValue]] .. clock .. [["}]]
print(body) 

To continue using a double quoted string, your variable assignment would look like the following (note how the quote after stringValue is not escaped):

body = "{\"name\":\"stringValue" .. clock .. "\"}"
Sign up to request clarification or add additional context in comments.

1 Comment

Single quotes would work here as well. Also you failed to address the crucial change to the OPs code that makes this work (you made the change and fixed the problem you just didn't mention it at all).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.