AddOrdered (Stringlist function)

From m204wiki
Revision as of 19:40, 14 July 2011 by JALWiccan (talk | contribs) (1 revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Add an item to an ordered Stringlist (Stringlist class)

This method adds an item to a (presumably) ordered Stringlist, inserting the item at the proper position to maintain the Stringlist's order. AddOrdered accepts one argument and returns the item number of the inserted string. AddOrdered is a callable method.


[%number =] sl:AddOrdered( string)

Syntax terms

%number A numeric variable to contain the item number of the added string in the Stringlist.
sl A Stringlist object.
string A string that is to be added to the Stringlist.

Usage notes

  • All errors in AddOrdered result in request cancellation.
  • AddOrdered assumes that the Stringlist is already in EBCDIC order, so it does a pseudo binary search to locate the correct insertion point. AddOrdered does not validate that the Stringlist is in order; if it is not in order, the insertion point is unpredictable.
  • AddOrdered always adds the indicated string, even if that string already exists on the Stringlist, with the new item being inserted so that the Stringlist is maintained in EBCDIC order.
  • Because AddOrdered will insert items into the middle of a Stringlist, it is susceptible to the same page-splitting, sparse Stringlist leaf-page issues as the Insert method.


The following code builds a sorted output Stringlist from an input Stringlist:

for %i from 1 to %inlist:count %string = %inlist:item(%i) %outlist:addOrdered(%string) end for

While it is generally more efficient to simply copy and sort (via the SortNew method) the input Stringlist, the technique above might be useful if the target Stringlist already has a large number of items.

See also