#### 37.3.3 Incrementing and decrementing macro variables

You can increment the value of a macro variable by 1 (one) like this:

    <$$myvar++>      (or just <$$myvar+>)

or decrement the value by 1 like this:

    <$$myvar-->      (or just <$$myvar->)

For example, to count Body paragraphs in a DITA file for HTML output, incrementing the count before using it:

    [HTMLParaStyles]
    Body=CodeStart

    [ParaStyleCodeStart]
    Body=<!-- this is <$$bodynum++ as %0.3d> -->

    [MacroVariables]
    bodynum=bp000

These settings result in a comment like the following for each instance of a Body paragraph in the HTML output:

    <!-- this is bp003 -->

Reserve enough digits

You must include enough placeholder digits in the starting value (in this example, bp000) to accommodate the range of values you expect in the file. If you do not, the number will roll over to zero after it reaches its maximum value: in this example bp999 would increment to bp000. If the value has no digits at all at the end, the last letter is incremented instead; so a starting value of aaa increments to aab, aac, ..., aaz, aba, ..., zzz, aaa. Case is retained for the incremented (or decremented) letter.

To increment the value after use, move the incrementing code after the reference:

    [ParaStyleCodeStart]
    Body=<!-- this is <$$bodynum> --><$$bodynum++>

Increment by assignment

You can use an assignment (see §37.3.2 Assigning values to macro variables) as another form of increment, as in the following:

    <$$myvar = ($$myvar + 1)>

This form does not require reserving the maximum number of digits first.

Increment hexadecimal numbers

Incrementing and decrementing using ++ or -- notation does not work with values stored as hexadecimal numbers; for those you must use an assignment to increment or decrement:

     <$$myhex = ($$myhex + 1)>

Display an increment

You can also display the value of an increment or decrement by adding as and a printf() format; for example:

    <$$myvar++ as %d>
    <$$myvar = ($$myvar + 1) as %0.4d>

See §37.6.3 Displaying expression results in output for information about display formats.

Increment indirectly

You can increment a variable indirectly:

    <$$myvar = "$$other">
    <*$$myvar++>

This sequence increments the value of $$other rather than the value of $$myvar. See §37.6.7 Using indirection in expressions.

Previous Topic: 37.3.2 Assigning values to macro variables

Next Topic: 37.3.4 Using predefined macro variables

Parent Topic: 37.3 Using macro variables

Sibling Topics:

37.3.1 Creating and invoking macro variables

37.3.2 Assigning values to macro variables

37.3.4 Using predefined macro variables

37.3.5 Creating macro variables from paragraph content