Values and arithmetic
Every value in MaSH has a type. The type decides what you can do with it. You can add two numbers, but adding two pieces of text sticks them together.
Variables
Section titled “Variables”You create a variable by giving a name a value:
set customer to "Acme Ltd"set orders to 12printline "{{customer}} has {{orders}} orders"Acme Ltd has 12 orderscustomer = "Acme Ltd"orders = 12printline("{{customer}} has {{orders}} orders")Acme Ltd has 12 ordersYou can change a variable later. The old value is simply replaced:
set orders to 12set orders to orders + 1printline orders13orders = 12orders = orders + 1printline(orders)13Names can use letters, numbers and underscores, and they’re case sensitive. orders and Orders are different variables.
Text, called a string, goes in double quotes. Join pieces of text with the ~ operator:
set first to "Ada"set last to "Lovelace"printline first ~ " " ~ lastAda Lovelacefirst = "Ada"last = "Lovelace"printline(first ~ " " ~ last)Ada LovelaceInterpolation does the same job and is often tidier:
set first to "Ada"set last to "Lovelace"printline "{{first}} {{last}}"Ada Lovelacefirst = "Ada"last = "Lovelace"printline("{{first}} {{last}}")Ada LovelaceSingle quotes make text that does not interpolate. This matters when you want a literal {{:
set x to 3printline "double: {{x}}"printline 'single: {{x}}'double: 3single: {{x}}x = 3printline("double: {{x}}")printline('single: {{x}}')double: 3single: {{x}}You can put double quotes inside the braces of a double-quoted string:
set name to "banana"printline "{{name.replace("a", "o")}}"bonononame = "banana"printline("{{name.replace("a", "o")}}")bononoWorking with text
Section titled “Working with text”A plain string has no methods of its own. Wrap it in mash.str() to get a set of helpers:
set title to mash.str(" Invoice INV-2041 for Acme ").trim()printline "{{title}}"printline "{{title.upper()}}"printline "{{title.length()}} characters"printline "{{title.replace('Acme', 'Beta')}}"printline title.startsWith("Invoice")printline title.containsAny("INV")Invoice INV-2041 for AcmeINVOICE INV-2041 FOR ACME25 charactersInvoice INV-2041 for Betatruetruetitle = mash.str(" Invoice INV-2041 for Acme ").trim()printline("{{title}}")printline("{{title.upper()}}")printline("{{title.length()}} characters")printline("{{title.replace('Acme', 'Beta')}}")printline(title.startsWith("Invoice"))printline(title.containsAny("INV"))Invoice INV-2041 for AcmeINVOICE INV-2041 FOR ACME25 charactersInvoice INV-2041 for BetatruetrueA few more you’ll use a lot: lower(), slice(start, length), truncate(max), split(separator), endsWith(), indexOf(), append() and prepend(). The full list is in the library reference.
A plain string takes these methods too, so "abc".upper() works without mash.str(). mash.str(text) is there when you want to be explicit.
Numbers
Section titled “Numbers”Numbers can be whole or have decimals, positive or negative:
set price to 20set vat to price * 0.2printline "VAT is {{vat}}"printline "Total is {{price + vat}}"VAT is 4Total is 24price = 20vat = price * 0.2printline("VAT is {{vat}}")printline("Total is {{price + vat}}")VAT is 4Total is 24The arithmetic operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | add | 7 + 2 | 9 |
- | subtract | 7 - 2 | 5 |
* | multiply | 7 * 2 | 14 |
/ | divide | 7 / 2 | 3.5 |
% | remainder | 7 % 2 | 1 |
** | power | 2 ** 10 | 1024 |
MaSH follows the usual order of operations. Use parentheses to change it:
printline 10 - 4 / 2printline (10 - 4) / 283printline(10 - 4 / 2)printline (10 - 4) / 283To change a variable by an amount, use a short form:
set count to 10increment count by 5decrement count by 3multiply count by 2printline countincrement countprintline count2425count = 10count += 5count -= 3count *= 2printline(count)count += 1printline(count)2425increment and decrement add or take away one. Both accept by, as in increment count by 10.
To show money or large numbers nicely, use mash.formatNumber:
printline mash.formatNumber(1234567.891, 2)1,234,567.89printline(mash.formatNumber(1234567.891, 2))1,234,567.89True, false and nothing
Section titled “True, false and nothing”A boolean is true or false. You get one whenever you compare things:
printline 5 is more than 3printline 5 is 3printline "a" is "A"truefalsefalseprintline(5 > 3)printline(5 == 3)printline("a" == "A")truefalsefalseComparing text is case sensitive, so "a" and "A" differ.
null means “no value”. A variable that was never set, or a field with nothing in it, is null. The ?? operator supplies a fallback when the left side is null:
set nickname to nullprintline nickname ?? "no nickname"no nicknamenickname = nullprintline(nickname ?? "no nickname")no nickname?? only replaces null. An empty string "" is a value, so "" ?? "x" stays empty. You’ll lean on ?? a lot when reading custom fields, which are often empty.
Asking what type something is
Section titled “Asking what type something is”printline mash.type(5)printline mash.type("hello")printline mash.type(1.5)printline mash.type(true)printline mash.type(null)integerstringdoublebooleanNULLprintline(mash.type(5))printline(mash.type("hello"))printline(mash.type(1.5))printline(mash.type(true))printline(mash.type(null))integerstringdoublebooleanNULLTry it
Section titled “Try it”Write a script that holds a net price of 80 and a VAT rate of 20 (per cent), then prints the gross price as Gross: £96.
In the next chapter your scripts start making choices.