AddUnique (Stringlist function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
 
mNo edit summary
Line 1: Line 1:
<span style="font-size:120%"><b>Conditionally add an item to a Stringlist</b></span>
{{Template:Stringlist:AddUnique subtitle}}


This callable method adds an item to a Stringlist if an identical item is not already there. It adds the item to the end of the Stringlist. AddUnique accepts one argument and returns one of the following:
This callable method adds an item to a Stringlist if an identical item is not already there. It adds the item to the end of the Stringlist. AddUnique accepts one argument and returns one of the following:
Line 10: Line 10:
AddUnique is a member of the [[Stringlist class]].
AddUnique is a member of the [[Stringlist class]].


==AddUnique Syntax==
==Syntax==
<pre>
{{Template:Stringlist:AddUnique syntax}}
[%rc =] %sl:AddUnique(string)
===Syntax terms===
</pre>
 
===Syntax Terms===
<dl>
<dl>
<dt>%rc<dd>A numeric variable to contain the item number of the added string, or the negative item number of an item that already contains the string.<dt>%sl<dd>A Stringlist object.<dt>string<dd>A string that is to be added to the Stringlist. </dl>
<dt>%rc<dd>A numeric variable to contain the item number of the added string, or the negative item number of an item that already contains the string.<dt>%sl<dd>A Stringlist object.<dt>string<dd>A string that is to be added to the Stringlist. </dl>


===Usage Notes===
==Usage notes==
<ul>
<ul>
<li>All errors in AddUnique result in request cancellation.
<li>All errors in AddUnique result in request cancellation.
Line 26: Line 23:
</ul>
</ul>


===Examples===
==Examples==
AddUnique returns the item number added (if no match was found) or the negative item number of the matching item (if one was found). This return code makes it easy to maintain a parallel Stringlist that contains, say, a count of the number of times a given value occurred, that is, was passed as a string to AddUnique. The following example illustrates such an approach:
AddUnique returns the item number added (if no match was found) or the negative item number of the matching item (if one was found). This return code makes it easy to maintain a parallel Stringlist that contains, say, a count of the number of times a given value occurred, that is, was passed as a string to AddUnique. The following example illustrates such an approach:



Revision as of 17:23, 31 December 2010

Conditionally add an item to a Stringlist (Stringlist class)


This callable method adds an item to a Stringlist if an identical item is not already there. It adds the item to the end of the Stringlist. AddUnique accepts one argument and returns one of the following:

  • The item number of the added string.
  • The negative of the item number that exactly matches the string being added.

AddUnique is a member of the Stringlist class.

Syntax

[%number =] sl:AddUnique( string)

Syntax terms

%rc
A numeric variable to contain the item number of the added string, or the negative item number of an item that already contains the string.
%sl
A Stringlist object.
string
A string that is to be added to the Stringlist.

Usage notes

  • All errors in AddUnique result in request cancellation.
  • AddUnique always adds the indicated string to the end of the Stringlist, but it does not add it if there is already an identical Stringlist item on the Stringlist.
  • AddUnique does not assume any order for the Stringlist, so it sequentially scans the entire Stringlist for matches to the string being added. Because of this, it is generally more expensive to use than AddUniqueOrdered for very large Stringlists. AddUniqueOrdered, however, may not be usable in all cases -- for example, if the target Stringlist starts out unordered.

Examples

AddUnique returns the item number added (if no match was found) or the negative item number of the matching item (if one was found). This return code makes it easy to maintain a parallel Stringlist that contains, say, a count of the number of times a given value occurred, that is, was passed as a string to AddUnique. The following example illustrates such an approach:

%in = %olist:addUnique(%data)
if %in gt 0 then
%clist:insert(%in, 1)
else
%in = -%in
%clist:replace(%in, %clist:item(%in) + 1)
end if