Mwnci Hashes

Hashes

Hashes are used to store data values in key:value pairs. A hash is a collection which is unordered, changeable and do not allow duplicates.

Hash items are presented in key:value pairs, and can be referred to by using the key name.

MyHash={
    "Name": "Eccles",
    "Age": 30
}
print(MyHash["Name"])

Eccles

Unordered

Unordered means that the items do not have a defined order, you cannot refer to an item by using an index.

Changeable

Hashes are changeable, meaning that we can change, add or remove items after the hash has been created.

Duplicates Not Allowed

Hashes cannot have two items with the same key:

MyHash={
"Name": "Eccles",
"Age": 30,
"Age": 31
}
print(MyHash)

{"Name": "Eccles", "Age": 31}

Hash Length

To determine how many items a hash has, use the len() builtin

println(len(MyHash))

2

Accessing a Hash

Get theĀ  Name value from the hash:

X = MyHash["Name"]

"Dalek"

Modify theĀ age item:

MyHash = set(MyHash, "Age", 32)

this can also be done using the following method:

MyHash = MyHash.set("Age", 32)

Looping through the Hash

The foreach loop can be used to loop through all the items of hash

Print each item in the Myhash hash:

foreach x in keys(MyHash) {
    println(MyHash[x])
}

Eccles
32

Removing Hash Items

You can use the delete() builtin command to remove an element from a hash.

MyHash=delete(MyHash, "Age")
or
MyHash=MyHash.delete("Age")

{"Name": "Eccles"}