REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 3-Aug-2010 Edit History  

REBOL 3 Functions: alter

alter  series  value  /case

If a value is not found in a series, append it; otherwise, remove it. Returns true if added. (Modifies)

Arguments:

series [series! port! bitset!]

value

Refinements:

/case - Case-sensitive comparison

See also:

find   remove   insert   unique   intersect   exclude   difference  

Description

The alter function helps you manage small data-sets. It either adds or removes a value depending on whether the value is already included. (The word alter is short for the word "alternate", the action taking place.)

For example, let's say you want to keep track of a few options used by your code. The options may be: flour, sugar, salt, and pepper. The following code will create a new block (to hold the data set) and add to it:

options: copy []
alter options 'salt
probe options
[salt]
alter options 'sugar
probe options
[salt sugar]

You can use functions like find to test the presence of an option in the set:

if find options 'salt [print "Salt was found"]
Salt was found

If you use alter a second time for the same option word, it will be removed:

alter options 'salt
probe options
[sugar]

Normally alter values are symbolic words (such as those shown above) but any datatype can be used such as integers, strings, etc.

alter options 120
alter options "test"
probe options
[sugar 120 "test"]

Also, alter returns true if the value was added to the series, or false if the value was removed.


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin