#### 37.4.5 Using pointers to process lists

The method described in §37.4.4 Using macros to process lists is fine for a single pair of lists. But what if you have many such pairs of lists? Using the actual names of the lists in the macro means including as many copies of the [Sidebar] macro as there are pairs of lists, even though the functionality is identical for all pairs. Instead, you can construct a macro that works for every pair of lists, using pointers (indirect references) to the lists instead of the literal names of the lists.

To create a pointer to a list, assign the list name, in quotes, to a macro variable:

<$$ptr="$$list">

A set-up macro could initialize the pointers, starting index, and ending index for the lists, then invoke the [Sidebar] macro, which is now generalized for any pair of lists:

[SetupMySidebar]
<$$fileptr="$$FM_File">
<$$textptr="$$SideTitle">\
<$$val=2><$$maxval=6>
<$Sidebar>

[Sidebar]
<$_while ($$val <= $$maxval)>\
<$_if ($$_currbase is *$$fileptr[$$val])>\
<p class="SidebarTxt"><*$$textptr[$$val]></p>\
<$_else>\
<p class="SidebarTxt"><a class="SidebarLnk"\
href="<*$$fileptr[$$val]>.htm"><*$$textptr[$$val]></a></p>\
<$_endif>
<$$val++>
<$_endwhile>

The asterisks in front of *fileptr[$$val] and *$$txtptr[$$val] indicate that these list variables are actually being used indirectly, as pointers to other lists. When you use the form *$$ptr[$$index], DITA2Go converts the reference internally to $$list[$$index] before retrieving the value.

Process a list of pointers

If you need to access a list of pointers, do it in two steps. Suppose you have a list that contains pointers to the other two lists in the sidebar example:

[PtrList]
1=$$DITA_File
2=$$SideTitles

To access an item in [DITA_File], first assign to a macro variable the pointer-list item that points to [DITA_File]:

<$$ptr=$$PtrList[1]>

Thereafter you can use the macro variable as a pointer to [DITA_File]; and so, referring to the [DITA_File] list in §37.4.4 Using macros to process lists, <*$$ptr[2]> gets you the second item in the list, descript. You do not use quotes around the value in this case, because you want the actual list item in $$ptr, not a reference to the list.

Previous Topic: 37.4.4 Using macros to process lists

Next Topic: 37.4.6 Using a list instead of a conditional expression

Parent Topic: 37.4 Using multiple-value list variables

Sibling Topics:

37.4.1 Understanding list-variable syntax

37.4.2 Assigning a value to a list-variable item

37.4.3 Initializing list variables

37.4.4 Using macros to process lists

37.4.6 Using a list instead of a conditional expression

Table of Contents • Index