Overview

Associative Arrays

Introduces associative arrays (dictionaries) in DWScript. It demonstrates the 'array [String] of T' syntax, basic key-value pair insertion, value retrieval, existence checking, and entry removal, highlighting the performance and convenience of indexed lookups.

Source Code

var ages: array [String] of Integer;

// Assigning values
ages['Alice'] := 30;
ages['Bob'] := 25;
ages['Charlie'] := 35;

// Accessing values
PrintLn('Alice is ' + IntToStr(ages['Alice']) + ' years old.');

// Checking for existence
if 'Bob' in ages then
  PrintLn('Bob is in the dictionary.');

// Count
PrintLn('Number of entries: ' + IntToStr(ages.Length));

// Removing an entry
ages.Delete('Charlie');
PrintLn('After deleting Charlie, length: ' + IntToStr(ages.Length));

Result

Alice is 30 years old.
Bob is in the dictionary.
Number of entries: 3
After deleting Charlie, length: 2
On this page