{"id":528,"date":"2024-11-08T11:27:51","date_gmt":"2024-11-08T11:27:51","guid":{"rendered":"http:\/\/monkey.ploogie.co.uk\/?page_id=528"},"modified":"2026-03-26T18:55:43","modified_gmt":"2026-03-26T18:55:43","slug":"mwnci-arrays","status":"publish","type":"page","link":"https:\/\/monkey.ploogie.co.uk\/index.php\/mwnci-arrays\/","title":{"rendered":"Mwnci Arrays"},"content":{"rendered":"<h2>What is an Array<\/h2>\n<p>An array is a collection of items stored at contiguous memory locations. Or in simpler terms it&#8217;s a variable that can hold more than one value.<\/p>\n<p>If you have a list of items, say Doctor Who enemies, storing them in an array would be like this:<\/p>\n<pre>Enemies = [\"Dalek\", \"Sontaran\", \"Cyberman\"]<\/pre>\n<p>An array can hold many values under a single name, and you can access the values by referring to an index number.<\/p>\n<h2>Accessing an Array<\/h2>\n<p>Get the value of the first array item:<\/p>\n<pre>X = Enemies[0]\r\n\r\n\"Dalek\"<\/pre>\n<p>Modify the first array item:<\/p>\n<pre>set(Enemies, 0, \"Silurian\")<\/pre>\n<p>this can also be done using the following method:<\/p>\n<pre>Enemies.set(0, \"Silurian\")<\/pre>\n<h2>The Length of an Array<\/h2>\n<p>The length of an array (the number of elements) is found by using the\u00a0<strong><em>len() <\/em><\/strong>builtin function.<\/p>\n<pre>X = len(Enemies)\r\n\r\n4<\/pre>\n<p>The length of an array is always one more than the highest array index.<\/p>\n<h2>Looping through the Array<\/h2>\n<p>The <em>foreach <\/em>loop can be used to loop through all the elements of an array<\/p>\n<p>Print each item in the <em>Enemies<\/em> array:<\/p>\n<pre>foreach x in Enemies {\r\n    println(x)\r\n}\r\n\r\nSilurian\r\nSontaran\r\nCyberman<\/pre>\n<h2>Adding Array Elements<\/h2>\n<p>You can use the <em>push() <\/em>builtin command to add an element to an array.<\/p>\n<pre>push(Enemies, \"Macra\")\r\nor\r\nEnemies.push(\"Macra\")\r\n\r\n[\"Silurian\", \"Sontaran\", \"Cyberman\", \"Macra\"]<\/pre>\n<h2>Removing Array Elements<\/h2>\n<p>You can use the <em>delete() <\/em>builtin command to remove an element from an array.<\/p>\n<pre>Enemies=delete(Enemies, 1)\r\nor\r\nEnemies.delete(1)\r\n\r\n[\"Silurian\", \"Cyberman\", \"Macra\"]<\/pre>\n<h2>Adding Array Elements<\/h2>\n<p>You can use the <em>push() <\/em>builtin command to add an element to an array.<\/p>\n<pre>push(Enemies, \"Macra\")\r\nor\r\nEnemies.push(\"Macra\")\r\n\r\n[\"Silurian\", \"Sontaran\", \"Cyberman\", \"Macra\"]<\/pre>\n<h2>Replacing Array Elements<\/h2>\n<p>You can use the <em>set() <\/em>builtin command to alter an element in an array.<\/p>\n<pre>Enemies=set(Enemies, 1, \"Weeping Angel\")\r\nor\r\nEnemies.set(1, \"Weeping Angel\")\r\n\r\n[\"Silurian\", \"Weeping Angel\", \"Macra\"]<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is an Array An array is a collection of items stored at contiguous memory locations. Or in simpler terms it&#8217;s a variable that can hold more than one value. If you have a list of items, say Doctor Who enemies, storing them in an array would be like this: Enemies = [&#8220;Dalek&#8221;, &#8220;Sontaran&#8221;, &#8220;Cyberman&#8221;] &hellip; <a href=\"https:\/\/monkey.ploogie.co.uk\/index.php\/mwnci-arrays\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Mwnci Arrays&#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-528","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/monkey.ploogie.co.uk\/index.php\/wp-json\/wp\/v2\/pages\/528","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=528"}],"version-history":[{"count":6,"href":"https:\/\/monkey.ploogie.co.uk\/index.php\/wp-json\/wp\/v2\/pages\/528\/revisions"}],"predecessor-version":[{"id":1422,"href":"https:\/\/monkey.ploogie.co.uk\/index.php\/wp-json\/wp\/v2\/pages\/528\/revisions\/1422"}],"wp:attachment":[{"href":"https:\/\/monkey.ploogie.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}