Alphanumerics

Get help using and writing Nisus Writer Pro macros.
Post Reply
adryan
Posts: 618
Joined: 2014-02-08 12:57:03
Location: Australia

Alphanumerics

Post by adryan »

G'day, all

Great to see more postings in the NWP Macros Forum. Here's another!

First, how can a Macro determine whether a response to a Prompt Input command is an integer or a letter of the alphabet?

Next, how does one increment a letter of the alphabet (eg, get a variable to step through a set of consecutive letters, beginning with a specified letter)?

Finally, what is the correct syntax for the following sample while expression, where x, y are previously declared variables?

Code: Select all

while ((x is not equal to 3) and (y is not equal to "z"))
Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
User avatar
phspaelti
Posts: 1356
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Alphanumerics

Post by phspaelti »

Hello Adrian,
adryan wrote: 2025-08-17 19:38:55 First, how can a Macro determine whether a response to a Prompt Input command is an integer or a letter of the alphabet?
The easiest way to do this kind of thing in Nisus is with Find:

Code: Select all

$prompt = Prompt Input
if $prompt.find('[A-Z]','Ei')
prompt "It's a letter"
elsif $prompt.find('\d','E')
prompt "It's a number"
end
In my example here the find will return @true if there is at least one letter in whatever the user types at the prompt. If you want to be sure the input is something more specific -- such as only a single letter -- you can write a more specific find expression.
Note that the result of the Prompt command will always be a text string, even if it consists of numerals, If you want to do calculations with the number you can cast it to an integer, but usually the calculation should work even without that.
Next, how does one increment a letter of the alphabet (eg, get a variable to step through a set of consecutive letters, beginning with a specified letter)?
You'll have to turn numbers into characters using the character code:

Code: Select all

for $code = 65 to 90
$let = Text.newWithCharacter($code)
prompt $let
end
You can get the code for a specified letter with:

Code: Select all

$code = $letter.codepoint(0)
The "0" is the index of the character for which you want to know the code. So "0" means the first character of the string in the variable $letter.
So if $letter contains the string "1", you will get the code "49". I hope that makes sense.
Finally, what is the correct syntax for the following sample while expression, where x, y are previously declared variables?

Code: Select all

while ((x is not equal to 3) and (y is not equal to "z"))
Basically as you have written it, or more precisely:

Code: Select all

while (($x != 3) && ($y != "z"))
Note that in this case Nisus will treat $x as a number, and $y as a string, but it hardly matters in this case.
philip
Post Reply