Arrays

DWScript supports several types of arrays: static (fixed size), dynamic (resizable), and associative (key-value maps).

Usage Examples

// Explicitly typed dynamic array
var arr : array of Integer;
arr.Add(10, 20, 30); // Add multiple values at once

// Array literal with range expansion
var nums := [1..3, 10, 5..1]; 
// Inferred as [1, 2, 3, 10, 5, 4, 3, 2, 1]

// Concatenation and Appending
var a := [1, 2] + [3, 4];
a += 5; // Append single value
a += [6, 7]; // Append another array

PrintLn(a.Map(IntToStr).Join(', '));
Result
1, 2, 3, 4, 5, 6, 7

Element Access & Manipulation

Arrays in DWScript are 0-indexed. You can access elements using square brackets. Dynamic arrays also support compound assignment operators directly on their elements.

var data : array of Integer := [10, 20, 30];

// Standard access
data[0] := 5;

// Compound assignment
data[1] += 5; // 25
data[2] *= 2; // 60

PrintLn(Format(
  '%d, %d, %d',
  [ data[0], data[1], data[2] ]
));
Result
5, 25, 60

Indexing Gotcha: Remember that while Arrays are 0-indexed, Strings are 1-based to maintain compatibility with classic Pascal. See the Strings documentation for details.

Iteration & Bounds

You can iterate through arrays using index-based loops or for..in. Index-based loops are generally faster and more explicit when you need the index or specific direction. for..in is convenient when you only need the values and want to avoid manual indexing.

var list := [10, 20, 30];

// Index-based iteration (fastest)
for var i := 0 to list.High do
  PrintLn(list[i].ToString);

// Value-based iteration (for..in)
for var val in list do
  PrintLn(val.ToString);
Result
10
20
30
10
20
30

Search & Membership

You can check if a value exists in an array using the .Contains() method or the more idiomatic in operator.

var list := [10, 20, 30];

// Using the method
if list.Contains(20) then
  PrintLn('Found 20');

// Using the operator (idiomatic)
if 30 in list then
  PrintLn('Found 30');
Result
Found 20
Found 30

Array Methods

Dynamic arrays come with a rich set of built-in methods for manipulation.

Method Description
Add(value) / Push Appends an element to the end. Push is typically used when treating the array as a stack.
Pop Removes and returns the last element.
Peek Returns the last element without removing it.
Insert(index, value) Inserts an element at a specific position.
Delete(index [, count]) Removes one or more elements by index.
Remove(value) Removes the first occurrence of a specific value.
SetLength(newLength) Resizes the array.
Clear Removes all elements.
Sort Sorts the array in-place. Supports an optional lambda for custom comparison.
Reverse Reverses the array in-place.
IndexOf(value [, from]) Returns the index of the first occurrence (at or after from), or -1 if not found.
Contains(value) Returns True if the value exists in the array.

Semantic Aliases

While Add and Push perform the same operation (appending to the end), using Push and Pop conveys a specific architectural intent: that the array is being used as a Stack (LIFO - Last In, First Out).

Functional & Transformation Methods

DWScript dynamic arrays support functional programming patterns. These methods return a new array and do not modify the original.

Method Description
Map(lambda) Transforms each element using the provided function.
Filter(lambda) Returns an array containing only elements that satisfy the condition.
Join(separator) Concats all elements into a single string.
var nums : array of Integer := [1, 2, 3, 4, 5];

// Square numbers
var squares := nums.Map(lambda (x: Integer) => x * x); 
PrintLn(squares.Map(lambda (x: Integer) => x.ToString).Join(', '));

// Keep evens
var evens := nums.Filter(lambda (x: Integer) => (x mod 2) = 0);
PrintLn(evens.Map(lambda (x: Integer) => x.ToString).Join(', '));
Result
1, 4, 9, 16, 25
2, 4

Static Arrays

Static arrays have a fixed size defined at compile-time.

var matrix : array [0..2, 0..2] of Integer;

matrix[1, 1] := 5;
On this page