Repository navigation
Hash Maps
A hash maps keys to values. Entries have no guaranteed visit or display order.
Use [:] for an empty hash and [key: value] for entries.
let $stock: [String: Integer] = ["tea": 8, "coffee": 5]
$stock["tea"] = $stock["tea"] - 2
echo $stock["tea"] $stock["missing"] $stock.count
6 () 2
Missing keys read as (). Assignment inserts or replaces an entry. Unlike array
indexes, hash keys are values rather than positions. "1", 1, and true
remain different keys; equal numeric 1 and 1.0 share lookup identity.
[String: Integer] constrains both sides. As a value expression it constructs an
empty typed hash; [(String): (Integer)] stores Type objects instead.
.key_type and .value_type expose Type values for the collection contract.
Arrays, structs, and other supported values can be keys. The hash stores a key snapshot, so changing the variable used to construct a key does not move the entry:
let $coordinate = [2, 4]
let $markers = [$coordinate: "camp"]
$coordinate[0] = 9
echo $markers[[2, 4]] $markers[$coordinate]
camp ()
Strings compare by text rather than their executable marker. Named keys retain their distinct identity. Do not use a display String as a substitute for a key of a nominal type.
let $totals: [String: Integer] = ["east": 3, "west": 7]
let $sum = 0
for ($region, $count) in $totals { $sum = $sum + $count }
echo $sum
10
Each step supplies a two-item array, which can be kept as one value or
destructured. A hash value of () remains data inside that pair. Iteration uses
a private snapshot; writes to the source do not change pending entries.
Use .properties to inspect the hash itself, rather than confusing its interface
with the entries it contains.
Related: Arrays · Iteration · Reflection
Begin here
Values and expressions
- Language basics
- Variables and scope
- Numbers and logic
- Strings and paths
- Arrays
- Hash maps
- Ranges and iteration
- Control flow
- Match and type guards
Functions and types
- Functions and calls
- Closures and function types
- Methods and properties
- Structs
- Enums
- Named types and unions
- Conversions
- Types and properties
Shell and modules
- Pipelines
- Pipeline splits
- Pipeline formats
- Commands and exit status
- Redirection
- Environment and exported variables
- Modules and visibility
- Nested modules and directory packages
- Conditional imports and declarations
- Errors and cleanup
- Supervised processes and terminals
Library guides
- Standard library
- The implicit prelude
- Bytes, files, readers, and writers
- Explicit item streams
- JSON documents and typed mapping
- Sockets, DNS, and TLS
- HTTP clients
- HTTP servers
- Framing, server-sent events, and JSON-RPC
- Terminal colors, styles, and geometry
- Build an application console
Interactive use