Skip to content

Common idioms

Short answers to “how do I…” while writing a Script. Each one shows the shortest way the language already offers, with a link to the rule in the spec. Read the tour first for the language as a whole.

Each example is a set of statements from a Handler body. A --> comment shows what the line before it says. A test runs every example on this page through bun run northtalk.

Use the optional k of x, and otherwise for a default. An optional read gives Nothing for a missing key, or when x is itself Nothing, so there’s no need to test is not nothing first. An ordinary read of a key on Nothing raises wrong kind.

put {name: "Ann"} into customer
say the optional city of customer otherwise "unknown"
--> unknown
say the optional city of the optional address of customer otherwise "unknown"
--> unknown

An optional read works the same on a Host Object, such as an entity whose components are properties: the optional lockable of actor otherwise "none" gives "none" when its Object Kind doesn’t define lockable. So one spelling reads a key whether the Host models a thing as a map or as an object.

otherwise replaces only Nothing. False, 0 and "" are kept. See Nothing defaults and optional key reads.

Use a Path: x at /a/2/b reads through maps and lists in one step, counting list items from 1. A missing key or item, or Nothing on the way, gives Nothing, so otherwise supplies a default. A ${e} hole puts a computed key or position in the Path.

put {reviews: [{by: "Ann"}, {by: "Bo"}]} into product
say product at /reviews/2/by
--> Bo
put 3 into i
say product at /reviews/${i}/by otherwise "anon"
--> anon
put "Cy" into product at /reviews/1/by
say product at /reviews/1/by
--> Cy

Writing through a Path makes no parents: a missing step before the last raises wrong kind. See Paths and writing through a Path.

To write to a key held in a variable, put the key expression in parentheses: put v into the (k) of m. Missing keys are added, so the map needs no withPath.

put {} into scores
put "red" into team
put 3 into the (team) of scores
add 2 to the (team) of scores
say scores
--> {red: 5}

See keys and properties and writing keys.

replace p in c with e runs e once per match, with the pattern’s Captures bound. A template fill is one statement.

put {name: "Ann", place: "the docks"} into fields
put "Meet {name} at {place}." into note
replace <"{", slot: letters, "}"> in note with the (slot) of fields
say note
--> Meet Ann at the docks.

As an expression, replace gives the new text and leaves the original as it was. See replace.

Negative indexes count from the end, so -1 is the last chunk and 2..-1 is everything after the first. This works for every chunk kind.

say characters 2..-1 of "hello"
--> ello
say items 2..-1 of [10, 20, 30]
--> [20, 30]
say words -2..-1 of "one two three"
--> two three

Don’t guard a range against running past the end. A range is clipped to the chunks there are, and a range whose start comes after its end gives empty text or an empty list.

put "hi" into t
say characters 3..-1 of t is empty
--> true
say characters 2..10 of t
--> i

There is no open-ended 2.. form. 2..-1 already says it, and every Core runs it (#2090). See reading chunks.

There is no HyperTalk repeat with i from 1 to n. Count over a range with repeat for each. Both ends are included.

put ["a", "b", "c"] into parts
repeat for each i in 2..the length of parts
say i & ": " & item i of parts
end repeat
--> 2: b
--> 3: c

repeat with i … is the syntax error repeat with, reported at with (#2097). See repeat.

A later ask overwrites it. To keep an answer, name a Result Target with into. The answer goes into that name as well as into it.

ask clock to now into started
ask locale to compare "b", "a"
say started is not nothing
--> true
say it
--> 1

Result Targets also follow Command Calls, call statements and send … and wait. See Result Targets.