Basic Types

Basic Types

In MaSH a simple variable can be one of several basic types. A variable’s type determines what we can do with it. For example you can perform arithmetic operations on a number but not on a string of text.

Strings

A string is a piece of data which contains a sequence of characters which form some text. So that MaSH can recognise where your string begins and ends its contents must be surrounded by quotation marks " ".

Natural

set myString to "Hello from mash"

Standard

myString = "Hello from mash"

Interpolation

String or variable interpolation allows us to construct strings of text which contain other variables. An example of this would be creating a message for a user, inserting details such as their name before sending it to them. In MaSH you can insert other variables into a string using two curly braces {{ }} (also known as mustache templating). The following script shows a very simple example of doing this.

Natural

set username to "[email protected]"
set message to "Hello {{username}}"

printline message

Standard

username = "[email protected]"
message = "Hello {{username}}"

printline(message)

Output

Hello [email protected]

Note how we first declare our username as a separate variable and then reference it inside our message by placing it inside the double braces. Subsequently when message is used, MaSH looks up the value of the username variable and inserts it for us.

Numbers

A number in MaSH is, well, a number! They can be positive, negative, whole or decimal.

Natural

set myNumber to -20.5

Standard

myNumber = -20.5

Booleans

Booleans can contain either true or false.

Natural

set myBoolean to true

Standard

myBoolean = true

Arrays

Arrays are lists of values which are stored together and accessible using a single variable name. You construct an array by surrounding your values with square brackets [ ] and separating them with commas ,.

Natural

set myArray to ["apple", "banana", "coconut"]

Standard

myArray = ["apple", "banana", "coconut"]

Arrays can also contain other variables. For example…

Natural

set a to "apple"
set b to "banana"
set c to "coconut"

set myArray to [a, b, c]

Standard

a = "apple"
b = "banana"
c = "coconut"

myArray = [a, b, c]

We can retrieve a value from an array using its index or its position within the array. Array indexes start from 0 rather than 1 so to retrieve the first value from our array we would write myArray[0].

Natural

set a to "apple"
set b to "banana"
set c to "coconut"

set myArray to [a, b, c]

printline myArray[0]

Standard

a = "apple"
b = "banana"
c = "coconut"

myArray = [a, b, c]

printline(myArray[0])

Output

apple

Values in arrays can be changed in much the same way as normal variables by using their index.

Natural

set a to "apple"
set b to "banana"
set c to "coconut"

set myArray to [a, b, c]

set myArray[0] to "pear"

printline myArray

Standard

a = "apple"
b = "banana"
c = "coconut"

myArray = [a, b, c]

myArray[0] = "pear"

printline(myArray)

Output

Array [
	"pear",
	"banana",
	"coconut"
]

Ranges

Sometimes we need to create an array containing a series of numerical values. We can do this like so:-

Natural

set myValues to [1,2,3,4,5,6,7,8,9,10]

printline myValues

Standard

myValues = [1,2,3,4,5,6,7,8,9,10]

printline(myValues)

Output

Array [
	1,
	2,
	3,
	4,
	5,
	6,
	7,
	8,
	9,
	10
]

Remember though that we’re always trying to find ways to write less code. Ranges allow us to quickly create an array containing a set of values. The following script produces the same output as the previous but is far more concise.

Natural

set myValues to 1...10

printline myValues

Standard

myValues = 1...10

printline(myValues)

Output

Array [
	1,
	2,
	3,
	4,
	5,
	6,
	7,
	8,
	9,
	10
]

If we would like our values to begin at zero we can leave off the first number when we create our range.

Natural

set myValues to ...10

printline myValues

Standard

myValues = ...10

printline(myValues)

Output

Array [
	0,
	1,
	2,
	3,
	4,
	5,
	6,
	7,
	8,
	9,
	10
]

Dictionaries

Dictionaries are similar to arrays but instead of being indexed numerically they can be indexed using a string of text. They are a useful way of grouping related information within your scripts. Data is added to dictionaries as key-value pairs. For every piece of data that you add to a dictionary you must also supply a key by which it will be accessed. Dictionaries are constructed using curly braces { and }, keys and values are separated with a : and multiple pairs are separated with a comma ,. The following script shows our keys are name and occupation and our values are “Grace Hopper” and “Computer Scientist” respectively.

Natural

set myDictionary to { name: "Grace Hopper", occupation: "Computer Scientist" }

Standard

myDictionary = { name: "Grace Hopper", occupation: "Computer Scientist" }

Like arrays dictionaries can also contain other variables.

Natural

set fullName to "Grace Hopper"

set myDictionary to { name: fullName, occupation: "Computer Scientist" }

printline myDictionary

Standard

fullName = "Grace Hopper"

myDictionary = { name: fullName, occupation: "Computer Scientist" }

printline(myDictionary)

Output

Dictionary {
	"name": "Grace Hopper",
	"occupation": "Computer Scientist"
}

Remember we said that dictionary values are stored as key-value pairs? Let’s go ahead and retrieve a value from our dictionary using its key. We do this exactly as we do when retrieving values from arrays but we use the key instead of the index.

Natural

set fullName to "Grace Hopper"

set myDictionary to { name: fullName, occupation: "Computer Scientist" }

printline myDictionary["name"]

Standard

fullName = "Grace Hopper"

myDictionary = { name: fullName, occupation: "Computer Scientist" }

printline(myDictionary["name"])

Output

Grace Hopper

And just like arrays we can also set values inside dictionaries too. If we provide a new key which does not exist within the dictionary then it will be inserted. If we use an existing key then its value will be overwritten.

Natural

set fullName to "Grace Hopper"
set myDictionary to { name: "Grace Hopper", occupation: "unknown" }

# Give Grace a career

set myDictionary["occupation"] to "Computer Scientist"

# And add details of her place of birth
set myDictionary["born"] to "New York"

printline myDictionary

Standard

fullName = "Grace Hopper"
myDictionary = { name: "Grace Hopper"}

# This value will be changed
myDictionary["occupation"] = "Computer Scientist"

# This value will be inserted
myDictionary["born"] = "New York"

printline(myDictionary)

Output

Dictionary {
	"name": "Grace Hopper",
	"occupation": "Computer Scientist",
	"born": "New York"
}

Array to Dictionary conversion

Behind the scenes arrays and dictionaries in MaSH are very similar and can be used in much the same way. Note that if you make a change to an array that does not follow the current array structure, inserting a value for a key which does not exist for example, the array will be converted to a dictionary.

Natural

set myArray to ["a", "b", "c"]

set myArray[4] to "d"

# MaSH automatically converts myArray to a dictionary

printline myArray

Standard

myArray = ["a", "b", "c"]

myArray[4] = "d"

#myArray is now a dictionary

printline(myArray)

Output

Dictionary {
	"0": "a",
	"1": "b",
	"2": "c",
	"4": "d"
}

Empty variables - null

A variable which has no value and type is considered null. Whilst this may seem strange it becomes useful as your scripts get more complex.

Natural

# Even though this string is empty (it contains no characters) it is still considered a string

set myString to ""

# This is still a number even though its value is 0

set myNumber to 0

# This variable is type-less and empty

set myNullVariable to null

Standard

# Even though this string is empty (it contains no characters) it is still considered a string

myString = ""

# This is still a number even though its value is 0

myNumber = 0

# This variable is type-less and empty

myNullVariable = null

← All articles