Log in

View Full Version : How to "quote more than two levels of nested strings"


fvisagie
22nd September 2014, 19:58
In some situations the need/desire arises to quote strings more than two levels deep. For up to two levels of nested strings the approach of split-and-concatenate works, but I have not been able to get this working for additional levels.

Here's a simplified - and therefore somewhat artificial - example from a real case:
# Effective function call to realise:
# MessageClip("""StrReplace(tracemsg, "\n", Chr(10))""")
Eval("""
# Some lines of code
NOP()
# In the actual code MessageClip() is replaced by WriteFile(...):
MessageClip("StrReplace(tracemsg, """ + """'""" + "\n" + """'""" + """, Chr(10))")
# Some more lines of code
last
""")

This displays
StrReplace(tracemsg, '\n', Chr(10))
which only seems to need replacing """'""" with """"""" to produce working code. However, doing that leads to the error
Script error: `\' can only appear at the beginning or end of a line
((null), line 5, column 40)
(New File (1), line 9)
While I realise several work-arounds are available, from an experiential point of view I'd still like to learn whether (and how) it is possible doing this directly using nested literal strings.

Gavino
22nd September 2014, 20:39
# Quote: convert a string to a literal by adding (triple) quotes
function Quote(string s) {
q = chr(34)
q3 = q+q+q
return q3+s+q3
}
...
Eval("""
# Some lines of code
NOP()
MessageClip(""" + Quote("""StrReplace(tracemsg, "\n", Chr(10))""") + """)
# Some more lines of code
last
""")

fvisagie
23rd September 2014, 08:23
Thank you.