CINXE.COM

class Array | Raku Documentation

<!DOCTYPE html> <html lang="en" class="fontawesome-i2svg-active fontawesome-i2svg-complete" style="scroll-padding-top:60px"> <head> <title>class Array | Raku Documentation</title> <meta charset="UTF-8" /> <link href="/assets/images/Camelia.ico" rel="icon" type="image/x-icon"/> <link rel="stylesheet" href="/assets/css/Website.css"/> <link rel="stylesheet" href="/assets/css/typegraph-styling.css"/> <link rel="stylesheet" href="/assets/css/typegraph-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/typegraph-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/css/filtered-toc-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/filtered-toc-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/css/announce-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/css/announce-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/all.min.css"/> <link rel="stylesheet" href="/assets/css/listf-styling-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/listf-styling-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/options-search-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/css/options-search-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/page-styling-main.css"/> <link rel="stylesheet" href="/assets/css/css/page-styling-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/page-styling-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/css/chyronToggle-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/chyronToggle-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/css/centreToggle-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/centreToggle-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/tm-styling.css"/> <link rel="stylesheet" href="/assets/css/tm-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/tm-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/rainbow-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/rainbow-light.css" title="light"/> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@tarekraafat/autocomplete.js@10.2.7/dist/css/autoComplete.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-light.min.css" title="light" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css" title="dark" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="/assets/scripts/all.min.js"></script><script src="/assets/scripts/filter-script.js"></script><script src="/assets/scripts/tableManager.js"></script><script src="https://cdn.jsdelivr.net/npm/fuzzysort@2.0.4/fuzzysort.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@tarekraafat/autocomplete.js@10.2.7/dist/autoComplete.min.js"></script><script src="/assets/scripts/filtered-toc.js"></script><script src="/assets/scripts/options-search.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/haskell.min.js"></script><script src="/assets/scripts/page-styling.js"></script><script src="/assets/scripts/announcements.js"></script><script src="/assets/scripts/rainbow.js"></script> </head> <body class="has-navbar-fixed-top"> <div id="class_Array" class="top-of-page"></div> <nav class="navbar is-fixed-top is-flex-touch" role="navigation" aria-label="main navigation"> <div class="navbar-item" style="margin-left: auto;"> <div class="left-bar-toggle" title="Toggle Table of Contents & Index"> <label class="chyronToggle left"> <input id="navbar-left-toggle" type="checkbox"> <span class="text">Contents</span> </label> </div> </div> <div class="container is-justify-content-space-around"> <div class="navbar-brand"> <div class="navbar-logo"> <a class="navbar-item" href="/"> <img src="/assets/images/camelia-recoloured.png" alt="Raku" width="52.83" height="38"> </a> <span class="navbar-logo-tm">tm</span> </div> <a role="button" class="navbar-burger burger" aria-label="menu" aria-expanded="false" data-target="navMenu"> <span aria-hidden="true"></span> <span aria-hidden="true"></span> <span aria-hidden="true"></span> </a> </div> <div id="navMenu" class="navbar-menu"> <div class="navbar-start"> <a class="navbar-item" href="/introduction" title="Getting started, Tutorials, Migration guides"> Introduction </a> <a class="navbar-item" href="/reference" title="Fundamentals, General reference"> Reference </a> <a class="navbar-item" href="/miscellaneous" title="Programs, Experimental"> Miscellaneous </a> <a class="navbar-item" href="/types" title="The core types (classes) available"> Types </a> <a class="navbar-item" href="/routines" title="Searchable table of routines"> Routines </a> <a class="navbar-item" href="https://raku.org" title="Home page for community"> Raku<sup>®</sup> </a> <a class="navbar-item" href="https://web.libera.chat/#raku" title="IRC live chat"> Chat </a> <div class="navbar-item has-dropdown is-hoverable"> <a class="navbar-link"> More </a> <div class="navbar-dropdown is-right is-rounded"> <hr class="navbar-divider"> <a class="navbar-item js-modal-trigger" data-target="download-ebook"> Download E-Book (epub) </a> <hr class="navbar-divider"> <a class="navbar-item" href="/about"> About </a> <hr class="navbar-divider"> <a class="navbar-item has-text-red" href="https://github.com/raku/doc-website/issues"> Report an issue with this site </a> <hr class="navbar-divider"> <a class="navbar-item" href="https://github.com/raku/doc/issues"> Report an issue with the documentation content </a> <hr class="navbar-divider"> <label class="navbar-item centreToggle" title="Enable/Disable Announcements" style="--switch-width: 18"> <input id="cancelAnnouncements" type="checkbox"> <span class="text">Announcements</span> <span class="on">suppressed</span> <span class="off">allowed</span> </label> </div> </div> </div> <div class="navbar-end navbar-search-wrapper"> <div class="navbar-item"> <div class="field has-addons"> <div class="autoComplete_options"> <input class="control input" id="autoComplete" type="search" dir="ltr" spellcheck=false autocorrect="off" autocomplete="off" autocapitalize="off" placeholder="🔍 Type f to search for ..."> </div> <div class="control" title="Search options"> <a class="button is-primary js-modal-trigger" data-target="options-search-info"> <span class="icon"> <i class="fas fa-cogs"></i> </span> </a> </div> </div> </div> </div> <div id="options-search-info" class="modal"> <div class="modal-background"></div> <div class="modal-content"> <div class="box"> <p>The last search was: <span id="selected-candidate" class="ss-selected"></span></p> <div class="control is-grouped is-grouped-centered options-search-controls"> <label class="centreToggle" title="Include extra information (Alt-E)" style="--switch-width: 10.5"> <input id="options-search-extra" type="checkbox"> <span class="text">Extra info</span> <span class="on">yes</span> <span class="off">no</span> </label> <p>The search response can be shortened by excluding the extra information line (Alt-E)</p> <label class="centreToggle" title="Search engine type Strict/Loose (Alt-L)" style="--switch-width: 10.5"> <input id="options-search-loose" type="checkbox"> <span class="text">Search type</span> <span class="on">loose</span> <span class="off">strict</span> </label> <p> The search engine can perform a strict search (only the characters in the search box) or a loose search (Alt-L)</p> <label class="centreToggle" title="Search in headings (Alt-H)" style="--switch-width: 10.5"> <input id="options-search-headings" type="checkbox"> <span class="text">Headings</span> <span class="on">yes</span> <span class="off">no</span> </label> <p>Search through headings in all web-pages (Alt-H)</p> <label class="centreToggle" title="Search indexed items (Alt-I)" style="--switch-width: 10.5"> <input id="options-search-indexed" type="checkbox"> <span class="text">Indexed</span> <span class="on">yes</span> <span class="off">no</span> </label> <p>Search through all indexed items (Alt-I)</p> <label class="centreToggle" title="Search composite pages (Alt-C)" style="--switch-width: 10.5"> <input id="options-search-composite" type="checkbox"> <span class="text">Composite</span> <span class="on">yes</span> <span class="off">no</span> </label> <p>Search in the names of composite pages, which combine similar information from the main web pages (Alt-C)</p> <label class="centreToggle" title="Search primary sources (Alt-P)" style="--switch-width: 10.5"> <input id="options-search-primary" type="checkbox"> <span class="text">Primary</span> <span class="on">yes</span> <span class="off">no</span> </label> <p>Search through the names of the main web pages (Alt-P)</p> <label class="centreToggle" title="Open in new tab (Alt-Q)" style="--switch-width: 10.5"> <input id="options-search-newtab" type="checkbox"> <span class="text">New tab</span> <span class="on">yes</span> <span class="off">no</span> </label> <p>Once a search candidate has been chosen, it can be opened in a new tab or in the current tab (Alt-Q)</p> <p>If all else fails, an item is added to use the Google search engine on the whole site</p> <button class="button is-warning" id="options-search-reset-defaults">Clear options, reset to defaults</button> <p>Exit this page by pressing &lt;Escape&gt;, or clicking on X or on the background.</p> </div> </div> </div> <button class="modal-close is-large" aria-label="close"></button> </div> </div> <div id="download-ebook" class="modal"> <div class="modal-background"></div> <div class="modal-content"> <div class="box"> <p><a href="/RakuDocumentation.epub" download>RakuDocumentation.epub</a> is a work in progress e-book. It targets the <a href="https://www.w3.org/publishing/epub3/">EPUB v3 specification</a>. It needs testing on a variety of ereaders (some of which may still implicitly expect compliance with EPUB v2). The CSS definitely needs enhancing (especially for code snippets). The Ebook opens in a Calibre reader, which is available on all operating systems.</p> <p>Suggestions are welcome and should be addressed by opening an issue on the Raku/doc-website repository</p> <p>Exit this popup by pressing &lt;Escape&gt;, or clicking on X or on the background.</p> </div> </div> <button class="modal-close is-large" aria-label="close"></button> </div> <div id="announcement-modal" class="modal"> <div class="modal-background"></div> <div class="modal-content"> <div class="box"> <div id="raku-doc-announcement"></div> <p>For more see <a href="/announcements">Announcements page</a>.</p> <p>Exit this popup by pressing &lt;Escape&gt;, or clicking on X or on the background.</p> </div> </div> <button class="modal-close is-large" aria-label="close"></button> </div> </div> </nav> <div class="tile is-ancestor section"> <div class="page-edit"> <a class="button page-edit-button" href="https://github.com/Raku/doc/edit/main/doc/Type/Array.rakudoc" title="Edit this page.&#13;Commit: 890f49ea0 2024-07-08"> <span class="icon is-right"> <i class="fas fa-pen-alt is-medium"></i> </span> </a> </div> <div id="left-column" class="tile is-parent is-2 is-hidden"> <div id="left-col-inner"> <div class="tabs" id="tabs"> <ul> <li class="is-active" id="toc-tab"> <a>Table of Contents</a> </li> <li id="index-tab"> <a>Index</a> </li> </ul> </div> <div class="field"> <div class="control has-icons-right"> <input id="toc-filter" class="input" type="text" placeholder="Filter"> <span class="icon is-right has-text-grey"> <i class="fas fa-search is-medium"></i> </span> </div> </div> <div class="raku-sidebar"> <aside id="toc-menu" class="menu"> <ul class="menu-list"> <li><a href="#Methods">Methods</a></li> <ul> <li><a href="#method_gist">method gist</a></li> <li><a href="#method_pop">method pop</a></li> <li><a href="#method_push">method push</a></li> <li><a href="#method_append">method append</a></li> <li><a href="#method_elems">method elems</a></li> <li><a href="#method_clone">method clone</a></li> <li><a href="#method_flat">method flat</a></li> <li><a href="#method_shift">method shift</a></li> <li><a href="#routine_unshift">routine unshift</a></li> <li><a href="#routine_prepend">routine prepend</a></li> <li><a href="#routine_splice">routine splice</a></li> <li><a href="#method_shape">method shape</a></li> <li><a href="#method_default">method default</a></li> <li><a href="#method_of">method of</a></li> <li><a href="#method_dynamic">method dynamic</a></li> <li><a href="#method_List">method List</a></li> <li><a href="#method_Slip">method Slip</a></li> </ul> </aside> <aside id="index-menu" class="menu is-hidden"> </aside> </div> </div> </div> <div id="main-column" class="tile is-parent" style="overflow-x: hidden;"> <div id="main-col-inner"> <section class="raku page-header"> <div class="container px-4"> <div class="raku page-title has-text-centered"> class Array </div> <div class="raku page-subtitle has-text-centered"> <p>Sequence of itemized values</p> </div> </div> </section> <section class="raku page-content"><div class="container px-4"><div class="columns one-col"> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">class</span> <span class="highlite-TYPE">Array</span> <span class="highlite-KEYWORD">is</span> <span class="highlite-TYPE">List</span> {}</pre> </div> </div> <p>An <code>Array</code> is a <a href="/type/List"><code>List</code></a> which forces all its elements to be scalar containers, which means you can assign to array elements.</p><p><code>Array</code> implements <a href="/type/Positional"><code>Positional</code></a> and as such provides support for <a href="/language/subscripts">subscripts</a>.</p><p><strong>Note</strong> from version 6.d, <code>.raku</code> (<code>.perl</code> before version 2019.11) can be called on multi-dimensional arrays.</p><p>If you want to declare an <code>Array</code> of a specific type, you can do so using several different ways:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@foo</span> <span class="highlite-KEYWORD">of</span> <span class="highlite-TYPE">Int</span> <span class="highlite-OPERATOR">=</span> 33,44; <span class="highlite-COMMENT"># [33 44] </span><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@bar</span> <span class="highlite-KEYWORD">is</span> <span class="highlite-TYPE">Array</span>[<span class="highlite-TYPE">Int</span>] <span class="highlite-OPERATOR">=</span> 33,44; <span class="highlite-COMMENT"># [33 44] </span></pre> </div> </div> <p>The second example, which parameterizes a type, is only available from Rakudo 2019.03.</p> <h1 id="Methods" class="raku-h1"><a href="#class_Array" title="go to top of document">Methods<a class="raku-anchor" title="direct link" href="#Methods">§</a></a></h1> <h2 id="method_gist" class="raku-h2"><a href="#class_Array" title="go to top of document">method gist<a class="raku-anchor" title="direct link" href="#method_gist">§</a></a></h2> <!-- defnmark method_gist 2 --> <p>Exactly the same as <a href="/type/List#method_gist"><code>List.gist</code></a>, except using square brackets for surrounding delimiters.</p> <h2 id="method_pop" class="raku-h2"><a href="#class_Array" title="go to top of document">method pop<a class="raku-anchor" title="direct link" href="#method_pop">§</a></a></h2> <!-- defnmark method_pop 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">pop</span>(Array:D:) <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">nodal</span></pre> </div> </div> <p>Removes and returns the last item from the array. Fails if the array is empty.</p><p>Like many <code>Array</code> methods, method <code>pop</code> may be called via the corresponding <a href="/type/independent-routines#sub_pop">subroutine</a>. For example:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@foo</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">a b</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-COMMENT"># a b </span><span class="highlite-NAME_ARRAY">@foo</span>.<span class="highlite-ROUTINE">pop</span>; <span class="highlite-COMMENT"># b </span><span class="highlite-ROUTINE">pop</span> <span class="highlite-NAME_ARRAY">@foo</span>; <span class="highlite-COMMENT"># a </span><span class="highlite-ROUTINE">pop</span> <span class="highlite-NAME_ARRAY">@foo</span>; <span class="highlite-KEYWORD">CATCH</span> { <span class="highlite-KEYWORD">default</span> { <span class="highlite-ROUTINE">put</span> .^<span class="highlite-ROUTINE">name</span><span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">: </span><span class="highlite-STRING_DELIMITER">'</span><span class="highlite-OPERATOR">,</span> .<span class="highlite-TYPE">Str</span> } }; <span class="highlite-COMMENT"># OUTPUT: «X::Cannot::Empty: Cannot pop from an empty Array␤»</span></pre> </div> </div> <h2 id="method_push" class="raku-h2"><a href="#class_Array" title="go to top of document">method push<a class="raku-anchor" title="direct link" href="#method_push">§</a></a></h2> <!-- defnmark method_push 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">push</span>(Array:D: <span class="highlite-OPERATOR">**</span><span class="highlite-NAME_ARRAY">@values</span> <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">raw</span> <span class="highlite-KEYWORD">--&gt;</span> Array:D) <span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">push</span>(Array:D: \<span class="highlite-ROUTINE">value</span> <span class="highlite-KEYWORD">--&gt;</span> Array:D) <span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">push</span>(Array:D: <span class="highlite-TYPE">Slip</span> \<span class="highlite-ROUTINE">values</span> <span class="highlite-KEYWORD">--&gt;</span> Array:D)</pre> </div> </div> <p>Adds the provided value or values to the end of the array, and returns the modified array. If any argument is a <a href="/type/Slip"><code>Slip</code></a>, method <code>push</code> will add the values produced by the argument's <a href="/routine/iterator">iterator</a>. It throws if the invocant array or a <a href="/type/Slip"><code>Slip</code></a> <a href="/routine/is-lazy">is lazy</a>.</p><p>Example:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@foo</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">a b c</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-NAME_ARRAY">@foo</span>.push: <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">d</span><span class="highlite-STRING_DELIMITER">'</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>; <span class="highlite-COMMENT"># OUTPUT: «[a b c d]␤»</span></pre> </div> </div> <p>Note that <code>push</code> does not attempt to flatten its argument list. If you pass an array or list as the thing to push, it becomes one additional element:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@a</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">a b c</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@b</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">d e f</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-NAME_ARRAY">@a</span>.push: <span class="highlite-NAME_ARRAY">@b</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a</span>.<span class="highlite-ROUTINE">elems</span>; <span class="highlite-COMMENT"># OUTPUT: «4␤» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a</span>[3].<span class="highlite-ROUTINE">join</span>; <span class="highlite-COMMENT"># OUTPUT: «def␤»</span></pre> </div> </div> <p>Multiple values are added to the array only if you supply them as separate arguments or in a <a href="/type/Slip"><code>Slip</code></a>:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@a</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">1</span><span class="highlite-STRING_DELIMITER">'</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a</span>.push: <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">a</span><span class="highlite-STRING_DELIMITER">'</span><span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">b</span><span class="highlite-STRING_DELIMITER">'</span>; <span class="highlite-COMMENT"># OUTPUT: «[1 a b]␤» </span><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@c</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">E F</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a</span>.push: <span class="highlite-NAME_ARRAY">@c</span>.<span class="highlite-TYPE">Slip</span>; <span class="highlite-COMMENT"># OUTPUT: «[1 a b E F]␤»</span></pre> </div> </div> <p>See <a href="/type/Array#method_append">method append</a> if you want to append multiple values that are produced by a single non-slipping <a href="/type/Iterable"><code>Iterable</code></a>.</p> <h2 id="method_append" class="raku-h2"><a href="#class_Array" title="go to top of document">method append<a class="raku-anchor" title="direct link" href="#method_append">§</a></a></h2> <!-- defnmark method_append 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">append</span>(Array:D: <span class="highlite-OPERATOR">**</span><span class="highlite-NAME_ARRAY">@values</span> <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">raw</span> <span class="highlite-KEYWORD">--&gt;</span> Array:D) <span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">append</span>(Array:D: \arg <span class="highlite-KEYWORD">--&gt;</span> Array:D)</pre> </div> </div> <p>Adds the provided values to the end of the array and returns the modified array, or throws if the invocant array or an argument that requires flattening <a href="/routine/is-lazy">is lazy</a>.</p><p>In contrast with <a href="/type/Array#method_push">method push</a>, method <code>append</code> adheres to the <a href="/language/functions#Slurpy_conventions">single argument rule</a> and is probably best thought of as:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">append</span>(Array:D: +<span class="highlite-ROUTINE">values</span> <span class="highlite-KEYWORD">--&gt;</span> Array:D)</pre> </div> </div> <p>This means that if you pass a <strong>single</strong> argument that is a non-<a href="/language/mop#VAR">itemized</a> <a href="/type/Iterable"><code>Iterable</code></a>, <code>append</code> will try to flatten it.</p><p>For example:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@a</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">a b c</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@b</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">d e f</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-NAME_ARRAY">@a</span>.append: <span class="highlite-NAME_ARRAY">@b</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a</span>.<span class="highlite-ROUTINE">elems</span>; <span class="highlite-COMMENT"># OUTPUT: «6␤» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a</span>; <span class="highlite-COMMENT"># OUTPUT: «[a b c d e f]␤»</span></pre> </div> </div> <h2 id="method_elems" class="raku-h2"><a href="#class_Array" title="go to top of document">method elems<a class="raku-anchor" title="direct link" href="#method_elems">§</a></a></h2> <!-- defnmark method_elems 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">elems</span>(Array:D: <span class="highlite-KEYWORD">--&gt;</span> Int:D)</pre> </div> </div> <p>Returns the number of elements in the invocant. Throws <a href="/type/X/Cannot/Lazy"><code>X::Cannot::Lazy</code></a> exception if the invocant <a href="/routine/is-lazy">is lazy</a>. For shaped arrays, returns the outer dimension; see <a href="/routine/shape">shape</a> if you need information for all dimensions.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-ROUTINE">say</span> [&lt;foo bar ber&gt;] .<span class="highlite-ROUTINE">elems</span>; <span class="highlite-COMMENT"># OUTPUT: «3␤» </span><span class="highlite-ROUTINE">say</span> (<span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@a</span>[42;3;70]).<span class="highlite-ROUTINE">elems</span>; <span class="highlite-COMMENT"># OUTPUT: «42␤» </span> <span class="highlite-KEYWORD">try</span> [<span class="highlite-OPERATOR">-</span>∞<span class="highlite-OPERATOR">...</span>∞].<span class="highlite-ROUTINE">elems</span>; <span class="highlite-ROUTINE">say</span> $<span class="highlite-OPERATOR">!</span>.^<span class="highlite-ROUTINE">name</span>; <span class="highlite-COMMENT"># OUTPUT: «X::Cannot::Lazy␤»</span></pre> </div> </div> <h2 id="method_clone" class="raku-h2"><a href="#class_Array" title="go to top of document">method clone<a class="raku-anchor" title="direct link" href="#method_clone">§</a></a></h2> <!-- defnmark method_clone 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">clone</span>(Array:D: <span class="highlite-KEYWORD">--&gt;</span> Array:D)</pre> </div> </div> <p>Clones the original <code>Array</code>. Modifications of elements in the clone are not propagated to the original and vice-versa:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@a</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">a b c</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@b</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-NAME_ARRAY">@a</span>.<span class="highlite-ROUTINE">clone</span>; <span class="highlite-NAME_ARRAY">@b</span>[1] <span class="highlite-OPERATOR">=</span> 42; <span class="highlite-NAME_ARRAY">@a</span>.push: 72; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@b</span>; <span class="highlite-COMMENT"># OUTPUT: «[a 42 c]␤» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a</span>; <span class="highlite-COMMENT"># OUTPUT: «[a b c 72]␤»</span></pre> </div> </div> <p>However, note that the reifier <em>is</em> shared between the two Arrays, so both Arrays will have the same elements even when each is randomly-generated on reification and each element will be reified just once, regardless of whether the reification was done by the clone or the original Array. <strong>Note:</strong> just as reifying an Array from multiple threads is not safe, so is, for example, reifying the clone from one thread while reifying the original from another thread is not safe.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@a</span> <span class="highlite-OPERATOR">=</span> 1<span class="highlite-OPERATOR">,</span> {<span class="highlite-ROUTINE">rand</span>} <span class="highlite-OPERATOR">…</span> ∞; <span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@b</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-NAME_ARRAY">@a</span>.<span class="highlite-ROUTINE">clone</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@b</span>[^3]; <span class="highlite-COMMENT"># OUTPUT: «(1 0.0216426755282736 0.567660896142156)␤» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a</span>[^3]; <span class="highlite-COMMENT"># OUTPUT: «(1 0.0216426755282736 0.567660896142156)␤»</span></pre> </div> </div> <h2 id="method_flat" class="raku-h2"><a href="#class_Array" title="go to top of document">method flat<a class="raku-anchor" title="direct link" href="#method_flat">§</a></a></h2> <!-- defnmark method_flat 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">flat</span>(Array:U:) <span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">flat</span>(Array:D:)</pre> </div> </div> <p>This method will return the type object itself if it's applied to a type object; when applied to an object, it will return a <a href="/type/Seq"><code>Seq</code></a> created from the <code>Array</code> underlying iterator.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@a</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">a 2 c</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a</span>.<span class="highlite-ROUTINE">flat</span>.^<span class="highlite-ROUTINE">name</span>; <span class="highlite-COMMENT"># OUTPUT: «Seq␤» </span></pre> </div> </div> <h2 id="method_shift" class="raku-h2"><a href="#class_Array" title="go to top of document">method shift<a class="raku-anchor" title="direct link" href="#method_shift">§</a></a></h2> <!-- defnmark method_shift 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">shift</span>(Array:D:) <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">nodal</span></pre> </div> </div> <p>Removes and returns the first item from the array. Fails if the array is empty.</p><p>Example:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@foo</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">a b</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>.<span class="highlite-ROUTINE">shift</span>; <span class="highlite-COMMENT"># OUTPUT: «a␤» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>.<span class="highlite-ROUTINE">shift</span>; <span class="highlite-COMMENT"># OUTPUT: «b␤» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>.<span class="highlite-ROUTINE">shift</span>; <span class="highlite-KEYWORD">CATCH</span> { <span class="highlite-KEYWORD">default</span> { <span class="highlite-ROUTINE">put</span> .^<span class="highlite-ROUTINE">name</span><span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">: </span><span class="highlite-STRING_DELIMITER">'</span><span class="highlite-OPERATOR">,</span> .<span class="highlite-TYPE">Str</span> } }; <span class="highlite-COMMENT"># OUTPUT: «X::Cannot::Empty: Cannot shift from an empty Array␤»</span></pre> </div> </div> <h2 id="routine_unshift" class="raku-h2"><a href="#class_Array" title="go to top of document">routine unshift<a class="raku-anchor" title="direct link" href="#routine_unshift">§</a></a></h2> <!-- defnmark routine_unshift 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> <span class="highlite-ROUTINE">unshift</span>(Array:D<span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">**</span><span class="highlite-NAME_ARRAY">@values</span> <span class="highlite-KEYWORD">--&gt;</span> Array:D) <span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">unshift</span>(Array:D: <span class="highlite-OPERATOR">**</span><span class="highlite-NAME_ARRAY">@values</span> <span class="highlite-KEYWORD">--&gt;</span> Array:D)</pre> </div> </div> <p>Adds the <code>@values</code> to the start of the array, and returns the modified array. Fails if <code>@values</code> is a lazy list.</p><p>Example:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@foo</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">a b c</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-NAME_ARRAY">@foo</span>.unshift: 1<span class="highlite-OPERATOR">,</span> 3 <span class="highlite-OPERATOR">...</span> 11; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>; <span class="highlite-COMMENT"># OUTPUT: «[(1 3 5 7 9 11) a b c]␤»</span></pre> </div> </div> <p>The notes in <a href="/type/Array#method_push">the documentation for method push</a> apply, regarding how many elements are added to the array.</p><p>The <a href="/type/Array#routine_prepend">routine prepend</a> is the equivalent for adding multiple elements from one list or array.</p> <h2 id="routine_prepend" class="raku-h2"><a href="#class_Array" title="go to top of document">routine prepend<a class="raku-anchor" title="direct link" href="#routine_prepend">§</a></a></h2> <!-- defnmark routine_prepend 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">sub</span> <span class="highlite-ROUTINE">prepend</span>(\array<span class="highlite-OPERATOR">,</span> |<span class="highlite-ROUTINE">values</span>) <span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">prepend</span>(Array:D: \<span class="highlite-ROUTINE">values</span>) <span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">prepend</span>(Array:D: <span class="highlite-OPERATOR">**</span><span class="highlite-NAME_ARRAY">@values</span> <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">raw</span>)</pre> </div> </div> <p>Adds the elements from <code>values</code> to the front of the array, modifying it in-place.</p><p>Example:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@foo</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">a b c</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-NAME_ARRAY">@foo</span>.prepend: 1<span class="highlite-OPERATOR">,</span> 3 <span class="highlite-OPERATOR">...</span> 11; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>; <span class="highlite-COMMENT"># OUTPUT: «[1 3 5 7 9 11 a b c]␤»</span></pre> </div> </div> <p>The difference from method <code>unshift</code> is that if you prepend a <strong>single</strong> array or list argument, <code>prepend</code> will flatten that array / list, whereas <code>unshift</code> prepends the list / array as just a single element.</p> <h2 id="routine_splice" class="raku-h2"><a href="#class_Array" title="go to top of document">routine splice<a class="raku-anchor" title="direct link" href="#routine_splice">§</a></a></h2> <!-- defnmark routine_splice 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> <span class="highlite-ROUTINE">splice</span>(<span class="highlite-NAME_ARRAY">@list</span><span class="highlite-OPERATOR">,</span> <span class="highlite-NAME_SCALAR">$start</span> <span class="highlite-OPERATOR">=</span> 0<span class="highlite-OPERATOR">,</span> <span class="highlite-NAME_SCALAR">$elems</span>?<span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">*</span><span class="highlite-NAME_ARRAY">@replacement</span> <span class="highlite-KEYWORD">--&gt;</span> <span class="highlite-TYPE">Array</span>) <span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">splice</span>(Array:D: <span class="highlite-NAME_SCALAR">$start</span> <span class="highlite-OPERATOR">=</span> 0<span class="highlite-OPERATOR">,</span> <span class="highlite-NAME_SCALAR">$elems</span>?<span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">*</span><span class="highlite-NAME_ARRAY">@replacement</span> <span class="highlite-KEYWORD">--&gt;</span> <span class="highlite-TYPE">Array</span>)</pre> </div> </div> <p>Deletes <code>$elems</code> elements starting from index <code>$start</code> from the <code>Array</code>, returns them and replaces them by <code>@replacement</code>. If <code>$elems</code> is omitted or is larger than the number of elements starting from <code>$start</code>, all the elements starting from index <code>$start</code> are deleted. If both <code>$start</code> and <code>$elems</code> are omitted, all elements are deleted from the <code>Array</code> and returned.</p><p>Each of <code>$start</code> and <code>$elems</code> can be specified as a <a href="/type/Whatever"><code>Whatever</code></a> or as a <a href="/type/Callable"><code>Callable</code></a> that returns an <a href="/type/Int"><code>Int</code></a>-compatible value: this returned value is then used as the corresponding argument to the <code>splice</code> routine.</p><p>A <a href="/type/Whatever"><code>Whatever</code></a> <code>$start</code> uses the number of elements of <code>@list</code> (or invocant). A <a href="/type/Callable"><code>Callable</code></a> <code>$start</code> is called with one argument—the number of elements in <code>@list</code> (or <code>self</code>).</p><p>A <a href="/type/Whatever"><code>Whatever</code></a> <code>$elems</code> deletes from <code>$start</code> to end of <code>@list</code> (or <code>self</code>) (same as no <code>$elems</code>). A <a href="/type/Callable"><code>Callable</code></a> <code>$elems</code> is called with one argument—the number of elements in <code>@list</code> (or <code>self</code>) minus the value of <code>$start</code>.</p><p>Example:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@foo</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">a b c d e f g</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>.<span class="highlite-ROUTINE">splice</span>(2<span class="highlite-OPERATOR">,</span> 3<span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">M N O P</span><span class="highlite-STRING_DELIMITER">&gt;</span>); <span class="highlite-COMMENT"># OUTPUT: «[c d e]␤» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>; <span class="highlite-COMMENT"># OUTPUT: «[a b M N O P f g]␤»</span></pre> </div> </div> <p>It can be used to extend an array by simply splicing in more elements than the current size (since version 6.d)</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@foo</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">a b c d e f g</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>.<span class="highlite-ROUTINE">splice</span>(6<span class="highlite-OPERATOR">,</span> 4<span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">M N O P</span><span class="highlite-STRING_DELIMITER">&gt;</span>); <span class="highlite-COMMENT"># OUTPUT: «[g]␤» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>; <span class="highlite-COMMENT"># OUTPUT: «[a b c d e f M N O P]␤» </span></pre> </div> </div> <p>The following equivalences hold (assuming that <code>@a.elems ≥ $i</code>):</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>「text」 without highlighting</label> <div> <pre class="nohighlights"><pre class="nohighlights"> @a.push($x, $y) @a.splice: * , *, $x, $y @a.pop @a.splice: *-1, @a.shift @a.splice: 0 , 1, @a.unshift($x, $y) @a.splice: 0 , 0, $x, $y @a[$i] = $y @a.splice: $i , 1, $y, </pre> </pre> </div> </div> <p>As mentioned above, a <a href="/type/Whatever"><code>Whatever</code></a> or <a href="/type/Callable"><code>Callable</code></a> object can be provided for both the <code>$start</code> and <code>$elems</code> parameters. For example, we could use either of them to remove the second to last element from an array provided it's large enough to have one:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@foo</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING">a b c d e f g</span><span class="highlite-STRING_DELIMITER">&gt;</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>.splice: *-2<span class="highlite-OPERATOR">,</span> *-1; <span class="highlite-COMMENT"># OUTPUT: «[f]␤» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>; <span class="highlite-COMMENT"># OUTPUT: «[a b c d e g]␤» </span> <span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_CODE">&amp;start</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-KEYWORD">-&gt;</span> <span class="highlite-NAME_SCALAR">$n</span> { <span class="highlite-NAME_SCALAR">$n</span> <span class="highlite-OPERATOR">-</span> 2 }; <span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_CODE">&amp;elems-num</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-KEYWORD">-&gt;</span> <span class="highlite-NAME_SCALAR">$m</span> { <span class="highlite-NAME_SCALAR">$m</span> <span class="highlite-OPERATOR">-</span> 1 }; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>.splice: <span class="highlite-NAME_CODE">&amp;start</span><span class="highlite-OPERATOR">,</span> <span class="highlite-NAME_CODE">&amp;elems-num</span>; <span class="highlite-COMMENT"># OUTPUT: «[e]␤» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>; <span class="highlite-COMMENT"># OUTPUT: «[a b c d g]␤»</span></pre> </div> </div> <h2 id="method_shape" class="raku-h2"><a href="#class_Array" title="go to top of document">method shape<a class="raku-anchor" title="direct link" href="#method_shape">§</a></a></h2> <!-- defnmark method_shape 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">shape</span>() { (<span class="highlite-OPERATOR">*</span><span class="highlite-OPERATOR">,</span>) }</pre> </div> </div> <p>Returns the shape of the array as a list.</p><p>Example:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@foo</span>[2;3] <span class="highlite-OPERATOR">=</span> ( <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING"> 1 2 3 </span><span class="highlite-STRING_DELIMITER">&gt;</span><span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING"> 4 5 6 </span><span class="highlite-STRING_DELIMITER">&gt;</span> ); <span class="highlite-COMMENT"># Array with fixed dimensions </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@foo</span>.<span class="highlite-ROUTINE">shape</span>; <span class="highlite-COMMENT"># OUTPUT: «(2 3)␤» </span><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@bar</span> <span class="highlite-OPERATOR">=</span> ( <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING"> 1 2 3 </span><span class="highlite-STRING_DELIMITER">&gt;</span><span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">&lt;</span><span class="highlite-STRING"> 4 5 6 </span><span class="highlite-STRING_DELIMITER">&gt;</span> ); <span class="highlite-COMMENT"># Normal array (of arrays) </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@bar</span>.<span class="highlite-ROUTINE">shape</span>; <span class="highlite-COMMENT"># OUTPUT: «(*)␤»</span></pre> </div> </div> <h2 id="method_default" class="raku-h2"><a href="#class_Array" title="go to top of document">method default<a class="raku-anchor" title="direct link" href="#method_default">§</a></a></h2> <!-- defnmark method_default 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-KEYWORD">default</span></pre> </div> </div> <p>Returns the default value of the invocant, i.e. the value which is returned when trying to access an element in the <code>Array</code> which has not been previously initialized or when accessing an element which has explicitly been set to <a href="/type/Nil"><code>Nil</code></a>. Unless the <code>Array</code> is declared as having a default value by using the <a href="/type/Variable#trait_is_default">is default</a> trait the method returns the type object for <a href="/type/Any"><code>Any</code></a>.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@a1</span> <span class="highlite-OPERATOR">=</span> 1<span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">&quot;</span><span class="highlite-STRING">two</span><span class="highlite-STRING_DELIMITER">&quot;</span><span class="highlite-OPERATOR">,</span> 2.718; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a1</span>.<span class="highlite-KEYWORD">default</span>; <span class="highlite-COMMENT"># OUTPUT: «(Any)␤» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a1</span>[4]; <span class="highlite-COMMENT"># OUTPUT: «(Any)␤» </span> <span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@a2</span> <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">default</span>(17) <span class="highlite-OPERATOR">=</span> 1<span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">&quot;</span><span class="highlite-STRING">two</span><span class="highlite-STRING_DELIMITER">&quot;</span><span class="highlite-OPERATOR">,</span> 3; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a2</span>.<span class="highlite-KEYWORD">default</span>; <span class="highlite-COMMENT"># OUTPUT: «17␤» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a2</span>[4]; <span class="highlite-COMMENT"># OUTPUT: «17␤» </span><span class="highlite-NAME_ARRAY">@a2</span>[1] <span class="highlite-OPERATOR">=</span> <span class="highlite-TYPE">Nil</span>; <span class="highlite-COMMENT"># (resets element to its default) </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a2</span>[1]; <span class="highlite-COMMENT"># OUTPUT: «17␤» </span></pre> </div> </div> <h2 id="method_of" class="raku-h2"><a href="#class_Array" title="go to top of document">method of<a class="raku-anchor" title="direct link" href="#method_of">§</a></a></h2> <!-- defnmark method_of 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-KEYWORD">of</span>()</pre> </div> </div> <p>Returns the type constraint for the values of the invocant. By default, i.e. if no type constraint is given during declaration, the method returns <code>(Mu)</code>.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@a1</span> <span class="highlite-OPERATOR">=</span> 1<span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">two</span><span class="highlite-STRING_DELIMITER">'</span><span class="highlite-OPERATOR">,</span> 3.14159; <span class="highlite-COMMENT"># (no type constraint specified) </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a1</span>.<span class="highlite-KEYWORD">of</span>; <span class="highlite-COMMENT"># OUTPUT: «(Mu)␤» </span> <span class="highlite-KEYWORD">my</span> <span class="highlite-TYPE">Int</span> <span class="highlite-NAME_ARRAY">@a2</span> <span class="highlite-OPERATOR">=</span> 1<span class="highlite-OPERATOR">,</span> 2<span class="highlite-OPERATOR">,</span> 3; <span class="highlite-COMMENT"># (values must be of type Int) </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a2</span>.<span class="highlite-KEYWORD">of</span>; <span class="highlite-COMMENT"># OUTPUT: «(Int)␤» </span><span class="highlite-NAME_ARRAY">@a2</span>.push: <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">d</span><span class="highlite-STRING_DELIMITER">'</span>; <span class="highlite-KEYWORD">CATCH</span> { <span class="highlite-KEYWORD">default</span> { <span class="highlite-ROUTINE">put</span> .^<span class="highlite-ROUTINE">name</span><span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">: </span><span class="highlite-STRING_DELIMITER">'</span><span class="highlite-OPERATOR">,</span> .<span class="highlite-TYPE">Str</span> } }; <span class="highlite-COMMENT"># OUTPUT: «X::TypeCheck::Assignment: Type check failed in assignment to @a2; expected Int but got Str (&quot;d&quot;)␤»</span></pre> </div> </div> <h2 id="method_dynamic" class="raku-h2"><a href="#class_Array" title="go to top of document">method dynamic<a class="raku-anchor" title="direct link" href="#method_dynamic">§</a></a></h2> <!-- defnmark method_dynamic 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-KEYWORD">dynamic</span>(Array:D: <span class="highlite-KEYWORD">--&gt;</span> Bool:D)</pre> </div> </div> <p>Returns <code>True</code> if the invocant has been declared with the <a href="/routine/is dynamic">is dynamic</a> trait, that is, if it's a dynamic variable that can be accessed from the inner lexical scope without having been declared there.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@a</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@a</span>.<span class="highlite-KEYWORD">dynamic</span>; <span class="highlite-COMMENT"># OUTPUT: «False␤» </span> <span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@b</span> <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">dynamic</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@b</span>.<span class="highlite-KEYWORD">dynamic</span>; <span class="highlite-COMMENT"># OUTPUT: «True␤»</span></pre> </div> </div> <p>If you declare a variable with the <code>*</code> twigil <code>is dynamic</code> is implied.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@*b</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@*b</span>.<span class="highlite-KEYWORD">dynamic</span>; <span class="highlite-COMMENT"># OUTPUT: «True␤»</span></pre> </div> </div> <p>Please note that the <code>dynamic</code> trait is a property of the variable, not the content. If a <a href="/type/Scalar"><code>Scalar</code></a> dynamic variable contains an array, rules for this container will apply (and it will always return <code>False</code>).</p> <h2 id="method_List" class="raku-h2"><a href="#class_Array" title="go to top of document">method List<a class="raku-anchor" title="direct link" href="#method_List">§</a></a></h2> <!-- defnmark method_List 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-TYPE">List</span>(Array:D:)</pre> </div> </div> <p>Converts the array to a <a href="/type/List"><code>List</code></a>.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@array</span>= [1]; <span class="highlite-NAME_ARRAY">@array</span>[3]=3; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@array</span>.<span class="highlite-TYPE">List</span>; <span class="highlite-COMMENT"># OUTPUT: «(1 Nil Nil 3)␤» </span></pre> </div> </div> <p>The holes will show up as <a href="/type/Nil"><code>Nil</code></a>.</p> <h2 id="method_Slip" class="raku-h2"><a href="#class_Array" title="go to top of document">method Slip<a class="raku-anchor" title="direct link" href="#method_Slip">§</a></a></h2> <!-- defnmark method_Slip 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> <span class="highlite-KEYWORD">method</span> <span class="highlite-TYPE">Slip</span>(Array:D: <span class="highlite-KEYWORD">--&gt;</span> Slip:D)</pre> </div> </div> <p>Converts the array to a <a href="/type/Slip"><code>Slip</code></a>, filling the holes with the type value the <code>Array</code> has been defined with.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-TYPE">Int</span> <span class="highlite-NAME_ARRAY">@array</span>= [0]; <span class="highlite-NAME_ARRAY">@array</span>[3]=3; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@array</span>.<span class="highlite-TYPE">Slip</span>; <span class="highlite-COMMENT"># OUTPUT: «(0 (Int) (Int) 3)␤» </span></pre> </div> </div> <h1 id="typegraphrelations" class="raku-h1"><a href="#" title="go to top of document">Typegraph<a class="raku-anchor" title="direct link" href="#typegraphrelations">§</a></a></h1> <figure class="typegraph" > <figcaption>Type relations for <code>Array</code></figcaption> <svg width="303pt" height="332pt" viewBox="0.00 0.00 303.04 332.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 328)"> <title>raku&#45;type&#45;graph</title> <polygon fill="#ffffff" stroke="transparent" points="-4,4 -4,-328 299.04,-328 299.04,4 -4,4"/> <!-- Array --> <g id="node1" class="node"> <title>Array</title> <g id="a_node1"><a xlink:href="/type/Array" xlink:title="Array"> <ellipse fill="none" stroke="#000000" cx="132.55" cy="-18" rx="35.19" ry="18"/> <text text-anchor="middle" x="132.55" y="-14.3" font-family="FreeSans" font-size="14.00" fill="#000000">Array</text> </a> </g> </g> <!-- List --> <g id="node7" class="node"> <title>List</title> <g id="a_node7"><a xlink:href="/type/List" xlink:title="List"> <ellipse fill="none" stroke="#000000" cx="132.55" cy="-90" rx="27" ry="18"/> <text text-anchor="middle" x="132.55" y="-86.3" font-family="FreeSans" font-size="14.00" fill="#000000">List</text> </a> </g> </g> <!-- Array&#45;&gt;List --> <g id="edge1" class="edge"> <title>Array&#45;&gt;List</title> <path fill="none" stroke="#000000" d="M132.55,-36.3C132.55,-44.02 132.55,-53.29 132.55,-61.89"/> <polygon fill="#000000" stroke="#000000" points="129.05,-61.9 132.55,-71.9 136.05,-61.9 129.05,-61.9"/> </g> <!-- Mu --> <g id="node2" class="node"> <title>Mu</title> <g id="a_node2"><a xlink:href="/type/Mu" xlink:title="Mu"> <ellipse fill="none" stroke="#000000" cx="30.55" cy="-306" rx="27" ry="18"/> <text text-anchor="middle" x="30.55" y="-302.3" font-family="FreeSans" font-size="14.00" fill="#000000">Mu</text> </a> </g> </g> <!-- Any --> <g id="node3" class="node"> <title>Any</title> <g id="a_node3"><a xlink:href="/type/Any" xlink:title="Any"> <ellipse fill="none" stroke="#000000" cx="30.55" cy="-234" rx="28.7" ry="18"/> <text text-anchor="middle" x="30.55" y="-230.3" font-family="FreeSans" font-size="14.00" fill="#000000">Any</text> </a> </g> </g> <!-- Any&#45;&gt;Mu --> <g id="edge2" class="edge"> <title>Any&#45;&gt;Mu</title> <path fill="none" stroke="#000000" d="M30.55,-252.3C30.55,-260.02 30.55,-269.29 30.55,-277.89"/> <polygon fill="#000000" stroke="#000000" points="27.05,-277.9 30.55,-287.9 34.05,-277.9 27.05,-277.9"/> </g> <!-- Cool --> <g id="node4" class="node"> <title>Cool</title> <g id="a_node4"><a xlink:href="/type/Cool" xlink:title="Cool"> <ellipse fill="none" stroke="#000000" cx="30.55" cy="-162" rx="30.59" ry="18"/> <text text-anchor="middle" x="30.55" y="-158.3" font-family="FreeSans" font-size="14.00" fill="#000000">Cool</text> </a> </g> </g> <!-- Cool&#45;&gt;Any --> <g id="edge3" class="edge"> <title>Cool&#45;&gt;Any</title> <path fill="none" stroke="#000000" d="M30.55,-180.3C30.55,-188.02 30.55,-197.29 30.55,-205.89"/> <polygon fill="#000000" stroke="#000000" points="27.05,-205.9 30.55,-215.9 34.05,-205.9 27.05,-205.9"/> </g> <!-- Positional --> <g id="node5" class="node"> <title>Positional</title> <g id="a_node5"><a xlink:href="/type/Positional" xlink:title="Positional"> <ellipse fill="none" stroke="#6666ff" cx="132.55" cy="-162" rx="53.89" ry="18"/> <text text-anchor="middle" x="132.55" y="-158.3" font-family="FreeSans" font-size="14.00" fill="#6666ff">Positional</text> </a> </g> </g> <!-- Iterable --> <g id="node6" class="node"> <title>Iterable</title> <g id="a_node6"><a xlink:href="/type/Iterable" xlink:title="Iterable"> <ellipse fill="none" stroke="#6666ff" cx="249.55" cy="-162" rx="45.49" ry="18"/> <text text-anchor="middle" x="249.55" y="-158.3" font-family="FreeSans" font-size="14.00" fill="#6666ff">Iterable</text> </a> </g> </g> <!-- List&#45;&gt;Cool --> <g id="edge4" class="edge"> <title>List&#45;&gt;Cool</title> <path fill="none" stroke="#000000" d="M114.31,-103.51C98.65,-114.26 75.85,-129.91 57.98,-142.17"/> <polygon fill="#000000" stroke="#000000" points="55.93,-139.34 49.66,-147.88 59.89,-145.11 55.93,-139.34"/> </g> <!-- List&#45;&gt;Positional --> <g id="edge5" class="edge"> <title>List&#45;&gt;Positional</title> <path fill="none" stroke="#6666ff" d="M132.55,-108.3C132.55,-116.02 132.55,-125.29 132.55,-133.89"/> <polygon fill="#6666ff" stroke="#6666ff" points="129.05,-133.9 132.55,-143.9 136.05,-133.9 129.05,-133.9"/> </g> <!-- List&#45;&gt;Iterable --> <g id="edge6" class="edge"> <title>List&#45;&gt;Iterable</title> <path fill="none" stroke="#6666ff" d="M152.13,-102.72C169.7,-113.23 195.83,-128.86 216.61,-141.29"/> <polygon fill="#6666ff" stroke="#6666ff" points="215.06,-144.45 225.44,-146.58 218.65,-138.44 215.06,-144.45"/> </g> </g> </svg> <p class="fallback"> <a rel="alternate" href="/assets/typegraphs/Array.svg"> Expand chart above </a></p> </figure> </div></div></section> </div> </div> </div> <footer class="footer main-footer"> <div class="container px-4"> <nav class="level"> <div class="level-left"> <div class="level-item"> <a href="/about">About</a> </div> <div class="level-item"> <a id="toggle-theme">Toggle theme</a> </div> <div class="level-item" title="890f49ea0 2024-07-08"> <a>Commit</a> </div> </div> <div class="level-right"> <div class="level-item"> <a href="/license">License</a> </div> </div> </nav> </div> </footer> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10