{"id":533,"date":"2024-11-08T13:04:21","date_gmt":"2024-11-08T13:04:21","guid":{"rendered":"http:\/\/monkey.ploogie.co.uk\/?page_id=533"},"modified":"2026-03-26T19:23:41","modified_gmt":"2026-03-26T19:23:41","slug":"mwnci-hashes","status":"publish","type":"page","link":"https:\/\/monkey.ploogie.co.uk\/index.php\/mwnci-hashes\/","title":{"rendered":"Mwnci Hashes"},"content":{"rendered":"<h2>Hashes<\/h2>\n<p>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.<\/p>\n<p>Hash items are presented in key:value pairs, and can be referred to by using the key name.<\/p>\n<pre>MyHash={\r\n    \"Name\": \"Eccles\",\r\n    \"Age\": 30\r\n}\r\n<span class=\"pythoncolor\"><span class=\"pythonkeywordcolor\">print<\/span>(MyHash[<span class=\"pythonstringcolor\">\"Name\"<\/span>])\r\n\r\nEccles<\/span><\/pre>\n<h3>Unordered<\/h3>\n<p>Unordered means that the items do not have a defined order, you cannot refer to an item by using an index.<\/p>\n<h3>Changeable<\/h3>\n<p>Hashes are changeable, meaning that we can change, add or remove items after the hash has been created.<\/p>\n<h3>Duplicates Not Allowed<\/h3>\n<p>Hashes cannot have two items with the same key:<\/p>\n<pre>MyHash={\r\n\"Name\": \"Eccles\",\r\n\"Age\": 30,\r\n\"Age\": 31\r\n}\r\nprint(MyHash)\r\n\r\n{\"Name\": \"Eccles\", \"Age\": 31}<\/pre>\n<h2>Hash Length<\/h2>\n<p>To determine how many items a hash has, use the <em>len() <\/em>builtin<\/p>\n<pre>println(len(MyHash))\r\n\r\n2<\/pre>\n<h2>Accessing a Hash<\/h2>\n<p>Get the\u00a0 <em>Name <\/em>value from the hash:<\/p>\n<pre>X = MyHash[\"Name\"]\r\n\r\n\"Eccles\"<\/pre>\n<p>Modify the\u00a0<em>age<\/em> item:<\/p>\n<pre>set(MyHash, \"Age\", 32)<\/pre>\n<p>this can also be done using the following method:<\/p>\n<pre>MyHash.set(\"Age\", 32)<\/pre>\n<h2>Looping through the Hash<\/h2>\n<p>The <em>foreach <\/em>loop can be used to loop through all the items of hash<\/p>\n<p>Print each item in the <em>Myhash <\/em>hash:<\/p>\n<pre>foreach x in keys(MyHash) {\r\n    println(MyHash[x])\r\n}\r\n\r\nEccles\r\n32\r\n\r\n\r\nforeach K, V in MyHash {\r\n    println(\"Key=\", K,\" Value=\", V)\r\n}\r\n\r\nK=Age V=32\r\nK=Name V=Eccles<\/pre>\n<h2>Removing Hash Items<\/h2>\n<p>You can use the <em>delete() <\/em>builtin command to remove an element from a hash.<\/p>\n<pre>delete(MyHash, \"Age\")\r\nor\r\nMyHash.delete(\"Age\")\r\n\r\n{\"Name\": \"Eccles\"}<\/pre>\n<p>&nbsp;<\/p>\n<h2><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>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={ &#8220;Name&#8221;: &#8220;Eccles&#8221;, &#8220;Age&#8221;: 30 } print(MyHash[&#8220;Name&#8221;]) Eccles Unordered Unordered means that the &hellip; <a href=\"https:\/\/monkey.ploogie.co.uk\/index.php\/mwnci-hashes\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Mwnci Hashes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-533","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/monkey.ploogie.co.uk\/index.php\/wp-json\/wp\/v2\/pages\/533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monkey.ploogie.co.uk\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/monkey.ploogie.co.uk\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/monkey.ploogie.co.uk\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/monkey.ploogie.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=533"}],"version-history":[{"count":8,"href":"https:\/\/monkey.ploogie.co.uk\/index.php\/wp-json\/wp\/v2\/pages\/533\/revisions"}],"predecessor-version":[{"id":1424,"href":"https:\/\/monkey.ploogie.co.uk\/index.php\/wp-json\/wp\/v2\/pages\/533\/revisions\/1424"}],"wp:attachment":[{"href":"https:\/\/monkey.ploogie.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}