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.
Read a key that may be missing
Section titled “Read a key that may be missing”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 customersay the optional city of customer otherwise "unknown"--> unknownsay the optional city of the optional address of customer otherwise "unknown"--> unknownAn 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.
Reach into decoded JSON
Section titled “Reach into decoded JSON”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 productsay product at /reviews/2/by--> Boput 3 into isay product at /reviews/${i}/by otherwise "anon"--> anonput "Cy" into product at /reviews/1/bysay product at /reviews/1/by--> CyWriting through a Path makes no parents: a missing step before the last raises wrong kind. See Paths and writing through a Path.
Write a computed key
Section titled “Write a computed key”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 scoresput "red" into teamput 3 into the (team) of scoresadd 2 to the (team) of scoressay scores--> {red: 5}See keys and properties and writing keys.
Fill a template
Section titled “Fill a template”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 fieldsput "Meet {name} at {place}." into notereplace <"{", slot: letters, "}"> in note with the (slot) of fieldssay note--> Meet Ann at the docks.As an expression, replace gives the new text and leaves the original as it was. See replace.
Take the rest of a text
Section titled “Take the rest of a text”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"--> ellosay items 2..-1 of [10, 20, 30]--> [20, 30]say words -2..-1 of "one two three"--> two threeDon’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 tsay characters 3..-1 of t is empty--> truesay characters 2..10 of t--> iThere is no open-ended 2.. form. 2..-1 already says it, and every Core runs it (#2090). See reading chunks.
Count with a range
Section titled “Count with a range”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 partsrepeat for each i in 2..the length of parts say i & ": " & item i of partsend repeat--> 2: b--> 3: crepeat with i … is the syntax error repeat with, reported at with (#2097). See repeat.
Keep an answer
Section titled “Keep an answer”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 startedask locale to compare "b", "a"say started is not nothing--> truesay it--> 1Result Targets also follow Command Calls, call statements and send … and wait. See Result Targets.