Page 1 of 1
String interpolation question
Posted: 2022-03-20 02:33:02
by phspaelti
How can one interpolate a variable name between alphabetic letters?
I want to create a string with an optional symbol that needs to appear right before an alphabetic character. As an example imagine trying to create the string '+voice' or '-voice'. If I create a string variable $plusminus, which contains the '+' or '-' symbol, and then try to interpolate like this:
I get an error asking about the unknown variable '$plusminusvoice', and if I try
I get an extra space between the symbol and the 'voice'. Other combinations just give expected results, since nothing else counts as an escape,
Re: String interpolation question
Posted: 2022-03-20 06:53:13
by johseb
I guess the question is probably directed to Martin because, let's face it, he's probably the only one who can tell you something you don't know about NWP macro language

Anyway, I will humbly propose one of my usual kludges: how about inserting a special character e.g. Non-breaking zero width space or Zero width space joiner (maybe as a Unicode code point with the syntax \x{…}) between the variable name and the text?
If you're building a string that you're gonna display, paste, etc keeping the sign and the "voice" together could make sense.
Re: String interpolation question
Posted: 2022-03-20 10:04:22
by phspaelti
johseb, thank you!
Actually the zero-width space idea did occur to me. That would work, if it were just about the visual result. But since I need this for a case of mark-up I really need it without any space. However your idea did point me to one obvious solution, which is I could just use a double quoted string. Duh!
I did also come up with a kludge solution:
Code: Select all
$voice = @S'voice'
@S'$plusminus$voice'
Anyhow, if Martin, or some other knowledgeable person could let us know whether it's possible to do this (without the kludge) in @String literals, that would be much appreciated.
Re: String interpolation question
Posted: 2022-03-21 08:58:31
by martin
johseb wrote: ↑2022-03-20 06:53:13
something you don't know about NWP macro language
I was also surprised to see a question from philip, our resident macro expert!
phspaelti wrote: ↑2022-03-20 02:33:02
How can one interpolate a variable name between alphabetic letters?
The direct answer to the question is that there isn't a way to do this kind of interpolation. It would be nice if there was a syntax like "${plusminus}voice" to explicitly separate the variable name from the plain text, but there isn't. Right now a person must rely upon luck to delimit the variable name.
However, a person need not use interpolation to construct a string. You can use good old string concatenation to get this job done:
Code: Select all
$plusminus = '+'
$joined = $plusminus & 'voice'
I hope that's all you needed
Re: String interpolation question
Posted: 2022-03-21 09:24:22
by phspaelti
Thanks Martin,
Yes, same as with johseb's suggestion, somehow I was so caught up in a particular idea, that I didn't think of the obvious. Now that I think it over, there are so many ways to do it…
