Example

Note About Series Data

Author: Carl Sassenrath
Return to REBOL Cookbook

Here is an important point for beginners to understand.

When you write a function that has a data series such as a block or string, the data is persistent over all calls to the function.

This is easier to understand with an example:


    example: func [item /local data] [
        data: [0]
        append data item
        print data
    ]

Now use the function from the console:


    >> example 1
    0 1
    >> example 2
    0 1 2
    >> example 3
    0 1 2 3

If you don't remember this behavior, it's going to surprise you when you least expect it. If you don't want it to happen, you can COPY the series before you use it:


    example: func [item /local data] [
        data: copy [0]
        append data item
        print data
    ]

This is also true for strings and other series datatypes.


    example: func [item /local data] [
        data: copy "Reb"
        append data item
        print data
    ]

Keep it in Mind

Note that this persistent nature of series data occurs nearly everywhere in REBOL. Keep it in mind.


2006 REBOL Technologies REBOL.com REBOL.net