Programming Utilities Guide

Conditional Testing

Arbitrary conditional testing is performed with the built-in ifelse(). In its simplest form

ifelse(a,b,c,d)

compares the two strings a and b. If a and b are identical, ifelse() returns the string c. Otherwise, string d is returned. Thus, a macro called compare() can be defined as one that compares two strings and returns yes or no, if they are the same or different:

define(compare, `ifelse($1, $2, yes, no)')

Notice the quotes, which prevent evaluation of ifelse() from occurring too early. If the final argument is omitted, the result is null, so

ifelse(a,b,c)

is c if a matches b, and null otherwise.

ifelse() can actually have any number of arguments and provides a limited form of branched decision capability. In the input

ifelse(a,b,c,d,e,f,g)

if the string a matches the string b, the result is c. Otherwise, if d is the same as e, the result is f. Otherwise, the result is g.