ARRAY

Synopsis
ARRAY size
(ARRAY size origin)
Description

Outputs an array of size items, each of which initially is an empty list. The size input must be a non-negative integer.

Array items can be selected with ITEM and changed with SETITEM. The first element in an array is item number 1, unless an origin input is given, in which case the first element of the array has that number as its index. If given, the origin input must be an integer. You can get the origin of an array with FIRST.

FMSLogo has a special syntax for creating arrays and specifying its members at the same time. Arrays are delimited by curly braces and the origin can be optionally specified by appending an @ character after the closing curly brace, followed by the origin. For example, {a b c}@0 creates an array with three items and an origin of 0. PRINT and friends render arrays using the same syntax.

Unlike lists, items in arrays can be modified after the array is created. This can lead to confusing behavior if the same array is referenced from multiple places. This is shown in the following program, which modifies an array in one list and, in doing so, also modifies it in a second list.

MAKE "array {a b c}
MAKE "list1 LIST 1 :array
MAKE "list2 LIST 2 :array
SHOW :list1
[1 {a b c}]

SHOW :list2
[2 {a b c}]

SETITEM 2 ITEM 2 :list1 "X
SHOW :list1
[1 {a X c}]

SHOW :list2
[2 {a X c}]

Example
MAKE "myarray (ARRAY 3 0)
SETITEM 2 :myarray 1
SETITEM 1 :myarray 2
SETITEM 0 :myarray 3
SHOW :myarray
{3 2 1}

SourceForge.net Logo