Skip to main content

ListNext

Retrieves elements sequentially from a list.

Synopsis

ListNext(list,ptr,value)

Parameters

list An expression that evaluates to a valid list. A Caché list must be created using ListBuild or ListFromString, or extracted from another list using List.
ptr A pointer to the next element in the list. You must specify ptr as a local variable initialized to 0 to point to the beginning of list. Caché increments ptr using an internal address value algorithm (not a predictable integer counter). Therefore, the only value you can use to set ptr is 0. ptr cannot be a global variable or a subscripted variable.
value A local variable used to hold the data value of a list element. value does not have to be initialized before invoking ListNext. value cannot be a global variable or a subscripted variable.

Description

ListNext sequentially returns elements from list. You initialize ptr to 0 before the first invocation of ListNext. This causes ListNext to begin returning elements from the beginning of the list. Each successive invocation of ListNext advances ptr and returns the next list element value to value. The ListNext function returns 1, indicating that a list element has been successfully retrieved.

When ListNext reaches the end of the list, it returns 0, resets ptr to 0, and leaves value unchanged from the previous invocation. Because ptr has been reset to 0, the next invocation of ListNext would start at the beginning of the list.

Caché Basic increments ptr using an internal address algorithm. Therefore, the only value you should use to set ptr is 0.

You can use ListValid to determine if list is a valid list. An invalid list causes ListNext to generate a <LIST> error.

Not all lists validated by ListValid can be used successfully with ListNext. When ListNext encounters a list element with a null value, it returns 1 indicating that a list element has been successfully retrieved, advances ptr to the next element, and resets value to be an undefined variable. This can happen with any of the following valid lists: value=ListBuild(), value=ListBuild(NULL), value=ListBuild(,), or when encountering an omitted list element, such as the second invocation of ListNext on value=ListBuild("a",,"b").

ListNext("",ptr,value) returns 0, and does not advance the pointer or set value. ListNext(ListBuild(""),ptr,value) returns 1, advances the pointer, and set value to the null string ("").

ListNext and Nested Lists

The following example returns three elements, because ListNext does not recognize the individual elements in nested lists:

mylist = ListBuild("Apple","Pear",ListBuild("Walnut","Pecan"))
ptr = 0
count = 0
   While ListNext(mylist,ptr,value)
    count=count+1
    PrintLn value
   Wend
 PrintLn "End of list: ",count," elements found"

Examples

The following example sequentially returns all the elements in the list:

mylist = ListBuild("Red","Blue","Green")
ptr = 0
count = 0
   While ListNext(mylist,ptr,value) 
    count = count+1
    PrintLn value
   Wend
   PrintLn "End of list: ",count," elements found"

See Also

FeedbackOpens in a new tab