String interpolation question

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
phspaelti
Posts: 1252
Joined: 2007-02-07 00:58:12
Location: Japan

String interpolation question

Post 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:

Code: Select all

@S'$plusminusvoice'
I get an error asking about the unknown variable '$plusminusvoice', and if I try

Code: Select all

@S'$plusminus voice'
I get an extra space between the symbol and the 'voice'. Other combinations just give expected results, since nothing else counts as an escape,
philip
johseb
Posts: 47
Joined: 2016-02-13 10:01:29

Re: String interpolation question

Post 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.
User avatar
phspaelti
Posts: 1252
Joined: 2007-02-07 00:58:12
Location: Japan

Re: String interpolation question

Post 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.
philip
User avatar
martin
Official Nisus Person
Posts: 5113
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: String interpolation question

Post 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
User avatar
phspaelti
Posts: 1252
Joined: 2007-02-07 00:58:12
Location: Japan

Re: String interpolation question

Post 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… :drunk:
philip
Post Reply