Skip to content

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.

You create a variable by giving a name a value:

set customer to "Acme Ltd"
set orders to 12
printline "{{customer}} has {{orders}} orders"
Acme Ltd has 12 orders

You can change a variable later. The old value is simply replaced:

set orders to 12
set orders to orders + 1
printline orders
13

Names 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 ~ " " ~ last
Ada Lovelace

Interpolation does the same job and is often tidier:

set first to "Ada"
set last to "Lovelace"
printline "{{first}} {{last}}"
Ada Lovelace

Single quotes make text that does not interpolate. This matters when you want a literal {{:

set x to 3
printline "double: {{x}}"
printline 'single: {{x}}'
double: 3
single: {{x}}

You can put double quotes inside the braces of a double-quoted string:

set name to "banana"
printline "{{name.replace("a", "o")}}"
bonono

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 Acme
INVOICE INV-2041 FOR ACME
25 characters
Invoice INV-2041 for Beta
true
true

A 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 can be whole or have decimals, positive or negative:

set price to 20
set vat to price * 0.2
printline "VAT is {{vat}}"
printline "Total is {{price + vat}}"
VAT is 4
Total is 24

The arithmetic operators:

OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23.5
%remainder7 % 21
**power2 ** 101024

MaSH follows the usual order of operations. Use parentheses to change it:

printline 10 - 4 / 2
printline (10 - 4) / 2
8
3

To change a variable by an amount, use a short form:

set count to 10
increment count by 5
decrement count by 3
multiply count by 2
printline count
increment count
printline count
24
25

increment 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.89

A boolean is true or false. You get one whenever you compare things:

printline 5 is more than 3
printline 5 is 3
printline "a" is "A"
true
false
false

Comparing 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 null
printline 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.

printline mash.type(5)
printline mash.type("hello")
printline mash.type(1.5)
printline mash.type(true)
printline mash.type(null)
integer
string
double
boolean
NULL

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.