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

REBOL 3 Functions: change

change  series  value  /part  length  /only  /dup  count

Changes a value in a series and returns the series after the change. (Modifies)

Arguments:

series [series! gob! port!] - Series at point to change

value [any-type!] - The new value

Refinements:

/part - Limits the amount to change to a given length or position

length [number! series! pair!]

/only - Only change a block as a single value (not the contents of the block)

/dup - Duplicates the change a specified number of times

count [number! pair!]

See also:

append   clear   insert   remove   sort  

Contents

Description

The change function modifies any type of series, such as a string! or block!, at its current index position.

It has many variations, but let's take a simple example that modifies a string! series:

name: "bog"
change name "d"
probe name
"dog"
change next name "i"
probe name
"dig"
change tail name "it"
probe name
"digit"

Here is an example that changes a block! series:

colors: [red green blue]
change colors 'gold
probe colors
[gold green blue]
change at colors 3 [silver orange teal]
probe colors
[gold green silver orange teal]

As you can see, if the second argument is a single value, it will replace the value at the current position in the first series. However, if the second argument is a series compatible with the first (block or string-based datatype), all of its values will replace those of the first argument or series.

Result

Be sure to note that change returns the series position just past the modification.

This allows you to cascade multiple changes.

For example:

test: "abcde"
change change test "1" "23"
probe test
"123de"

So, you must use head if you need the string at its starting position:

probe head change "bog" "d"
"dog"
probe head change [1 4 5] [1 2 3]
[1 2 3]
probe head change [123 "test"] "the"
["the" "test"]

Partial changes

The /PART refinement changes a specified number of elements within the target series.

In this line, the 2 indicates that the "ab" are both replaced with the new string, "123":

probe head change/part "abcde" "123" 2
"123cde"

Duplication

probe head change/dup "abc" "->" 5
"->->->->->"

Editor note: This section is new or has has recently changed and is still under construction.

title: copy "how it REBOL"
change title "N"
probe title
"Now it REBOL"
change find title "t" "s"
probe title
"Now is REBOL"
blk: copy ["now" 12:34 "REBOL"]
change next blk "is"
probe blk
["now" "is" "REBOL"]
probe head change/only [1 4 5] [1 2 3]
[[1 2 3] 4 5]
probe head change/only [1 4 5] [[1 2 3]]
[[[1 2 3]] 4 5]
string: copy "crush those grapes"
change/part string "eat" find/tail string "crush"
probe string
"eat those grapes"


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