Comma-separated lists are used to store multiple elements in a single character variable. For example, you might want to store:
  • Sales quotas for 12 months
  • The department names in your company
  • The names of the 12 months of the year

Define a list

A list is a string with substrings, each separated by a separator character. A comma is the default separator.

The following example code shows two examples of list variables. You do not need to initialize a list variable when it is defined, but it must have items in it in order for you to work with it.

VAR CHAR cMonths = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec".

VAR CHAR cQuarterlyQuotas = "100500,400000,250000,175000".

Access a value in a list

ABL provides functions to access and manipulate the items in a list. These functions allow you to:

  • Locate the position of an item in the list (LOOKUP function).
  • Extract a value from a particular position in the list (ENTRY function).
  • Find out how many items are in the list (NUM-ENTRIES function).
  • Set a particular item in a list to a value (ENTRY statement).

Look up an item in a list

Use the LOOKUP function to determine whether a specific item is in a list. The LOOKUP function returns 0 if the item is not in the list; otherwise, it returns an integer value of the item's position in the list.

The syntax for the LOOKUP function is:

position = LOOKUP(expression, list)
position
Returns an INTEGER value of the position of expression in list. If not found, then position is set to 0.
expression
A character expression to search for in list.
list

A CHARACTER variable that contains a comma-separated list of items.

Example: Use LOOKUP with a list

In the following example code, a list called cMonths is defined, which contain the months of the year. expr is the expression to search for in the list and is set to "Sep".

VAR CHAR cMonths = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec".
VAR CHAR expr = "Sep".
VAR INT position.

position = LOOKUP(expr, cMonths).

MESSAGE "cMonths:" cMonths SKIP
  "expr:" expr SKIP
  "position:" position VIEW-AS ALERT-BOX.

The following output is produced from running the code. Note that the position is set to 9, because "Sep" occurs in the ninth position in the list.

cMonths: Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec 
expr: Sep 
position: 9

Extract a value from a list

Use the ENTRY function to return an entry from a list based on an integer position.

item = ENTRY(position,list)

The following example code extracts address data from a comma-separated list.

VAR CHAR cAddress.

cAddress = "Lift Tours,276 North Drive,,Burlington,MA,01730,USA".

MESSAGE "Name:" ENTRY(1,cAddress) SKIP
  "Address line 1:" ENTRY(2,cAddress) SKIP
  "Address line 2:" ENTRY(3,cAddress) SKIP
  "City:" ENTRY(4,cAddress) SKIP
  "State:" ENTRY(5,cAddress) SKIP
  "Zipcode:" ENTRY(6,cAddress) SKIP
  "Country:" ENTRY(7,cAddress) VIEW-AS ALERT-BOX.

The following output is produced from running the code.

Name: Lift Tours 
Address line 1: 276 North Drive 
Address line 2:  
City: Burlington 
State: MA 
Zipcode: 01730 
Country: USA

Determine how many entries are in a list

Use the NUM-ENTRIES function to return how many items are in a list. NUM-ENTRIES is useful when you want to process items in the list in a loop. The value of NUM-ENTRIES tells you the number of iterations you need to do.

The example code uses NUM-ENTRIES to determine the number of times to iterate in the REPEAT block.

VAR INT ix = 1.	
VAR CHAR cMonths = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec".
  
REPEAT ix = 1 TO NUM-ENTRIES(cMonths):
  DISPLAY ix " " ENTRY(ix, cMonths).
END.

Running the code produces the following output:

        ix
----------
         1   Jan
         2   Feb
         3   Mar
         4   Apr
         5   May
         6   Jun
         7   Jul
         8   Aug
         9   Sep
        10   Oct
        11   Nov
        12   Dec

Set a value in a list

You can substitute a value in a list with a different value by using the ENTRY statement. The syntax for the ENTRY statement is:

ENTRY(position, list) = expr.
position
An integer value that corresponds to the position of a character string in a list.
list
A list of character strings separated with a character delimiter.
expr
A character value that replaces the item in the list at the specified position.

The following example code substitutes character strings in French.

VAR CHAR cList = "North,South,East,West".

MESSAGE "English:" cList VIEW-AS ALERT-BOX.

ENTRY(1, cList) = "Nord".
ENTRY(2, cList) = "Sud".
ENTRY(3, cList) = "Est".
ENTRY(4, cList) = "Ouest".

MESSAGE "French:" cList VIEW-AS ALERT-BOX.

Running the code produces the following output:

English: North,South,East,West
French: Nord,Sud,Est,Ouest