Copying a local series

We mentioned this already in the Copying a series section in Chapter 5Working with Series and Blocks, but as this is a pitfall for new Red programmers, we will stress it again here—a series (also called a string) local to a function should be initialized with a copy. If not, the series will keep its value over subsequent function calls, which is an unexpected result for newcomers. Here is a correct example of how to initialize a local series using a function:

correct: function[] [    data: copy []             ; correct initialization!    append data 108    data]correct ;== [108]correct ;== [108]correct ;== [108]

Run the snippet with data: [] to see for yourself!

Get Learn Red - Fundamentals of Red now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.