txt: ## subscripts.md
  ## Chunk 1 of 2
  Subscripts
  
  Accessing data structure elements by index or key
  
  One often needs to refer to a specific element (or slice of elements) from a collection or data structure. Borrowing from mathematical notation where the components of a vector *v* would be referred to as *v₁, v₂, v₃*, this concept is called "subscripting" (or "indexing") in Raku.
  
  # [Basics](#Subscripts "go to top of document")[§](#Basics "direct link")
  
  Raku provides two universal subscripting interfaces:
  
  |  | elements are identified by | interface name | supported by |
  | --- | --- | --- | --- |
  | [ ] | zero-based indices | Positional | Array, List, Buf, Match, ... |
  | { } | string or object keys | Associative | Hash, Bag, Mix, Match, ... |
  
  ## [`Positional`](/type/Positional) subscripting[§](#Positional_subscripting "direct link")
  
  [`Positional`](/type/Positional) subscripting (via [`postcircumfix [ ]`](/language/operators#postcircumfix_[_])) addresses elements of an ordered collection by their position. Index 0 refers to the first element, index 1 to the second, and so on:
  
  ```raku
  my @chores = "buy groceries", "feed dog", "wash car";
  say @chores[0];  # OUTPUT: «buy groceries␤»
  say @chores[1];  # OUTPUT: «feed dog␤»
  say @chores[2];  # OUTPUT: «wash car␤»
  ```
  
  Note that Rakudo's implementation of subscripting allows only 63 bits on 64 bit builds. See [Stack Overflow](https://stackoverflow.com/questions/61031107/do-array-indices-need-to-be-native-ints/61031617#61031617). If larger subscripts are required, one may use [Array::Sparse](https://raku.land/zef:lizmat/Array::Sparse) or alternatively, use a [`Hash`](/type/Hash) with [`Associative`](/type/Associative) subscripting (see below).
  
  ## [`Associative`](/type/Associative) subscripting[§](#Associative_subscripting "direct link")
  
  [`Associative`](/type/Associative) subscripting (via [`postcircumfix { }`](/language/operators#postcircumfix_{_})), does not require the collection to keep its elements in any particular order - instead, it uses a unique key to address each value. The nature of the keys depends on the collection in question: For example a standard [`Hash`](/type/Hash) uses string keys, whereas a [`Mix`](/type/Mix) allows arbitrary objects as keys, etc.:
  
  ```raku
  my %grade = Zoe => "C", Ben => "B+";
  say %grade{"Zoe"};  # OUTPUT: «C␤»
  say %grade{"Ben"};  # OUTPUT: «B+␤»
  
  my $stats = ( Date.today => 4.18, Date.new(2015,  4,  5) => 17.253 ).Mix;
  say $stats{ Date.new(2015, 4, 4) + 1 };  # OUTPUT: «17.253␤»
  ```
  
  For passing single-word string keys to `{ }`, you can also use the [angle bracketed word quoting constructs](/language/quoting#Word_quoting:_qw) as if they were postcircumfix operators:
  
  ```raku
  my %grade = Zoe => "C", Ben => "B+";
  say %grade<Zoe>;    # OUTPUT: «C␤»
  say %grade<Ben>;    # OUTPUT: «B+␤»
  ```
  
  This is really just syntactic sugar that gets turned into the corresponding `{ }` form at compile-time:
  
  ```raku
  %hash<foo bar>;       # same as %hash{ <foo bar> }
  %hash«foo "$var"»;    # same as %hash{ «foo "$var"» }
  %hash<<foo "$var">>;  # same as %hash{ <<foo "$var">> }
  
  ```
  
  You may have noted above that we avoided having to quote `Zoe` by using the `=>` operator, but that same operator did not just put invisible quotes around `Date.new(2015, 4, 5)`, and we were able to find the same element using `$stats{ Date.new(2015, 4, 4) + 1 }`. This is because `=>` only puts invisible quotes around single words, and by "word" we mean an identifier/name. The `=>` operator is there to prevent us from accidentally calling functions or using constants with that name.
  
  Hash subscripts do not do the same thing as `=>`. The default [`Hash`](/type/Hash) has been made to behave the way new users have come to expect from using other languages, and for general ease of use. On a default [`Hash`](/type/Hash), subscripts coerce keys into strings, as long as those keys produce something [`Cool`](/type/Cool). You can use `.raku` on a collection to be sure whether the keys are strings or objects:
  
  ```raku
  ( 1  => 1 ).raku.say;            # OUTPUT: «1 => 1␤»
  my %h; %h{1}   = 1; say %h.raku; # OUTPUT: «{ "1" => 1 }␤»
  ( 1/2 => 1 ).raku.say;           # OUTPUT: «0.5 => 1␤»
  my %h; %h{1/2} = 1; say %h.raku; # OUTPUT: «{ "0.5" => 1 }␤»
  ( pi => 1 ).raku.say;            # OUTPUT: «:pi(1)␤»
  my %h; %h{pi}  = 1; say %h.raku; # OUTPUT: «{ "3.14159265358979" => 1 }␤»
  ```
  
  While the invisible quotes around single names is built into `=>`, string conversion is not built into the curly braces: it is a behavior of the default [`Hash`](/type/Hash). Not all types of hashes or collections do so:
  
  ```raku
  my %h := MixHash.new;
  %h{pi} = 1; %h.raku.say;         # OUTPUT: «(3.14159265358979e0=>1).MixHash␤»
  
  ```
  
  (Any name that `=>` would convert to a string can also be used to build a pair using "adverbial notation" and will appear that way when viewed through `.raku`, which is why we see `:pi(1)` above.)
  
  ## [Applying subscripts](#Subscripts "go to top of document")[§](#Applying_subscripts "direct link")
  
  Subscripts can be applied to any expression that returns a subscriptable object, not just to variables:
  
  ```raku
  say "__Hello__".match(/__(.*)__/)[0];   # OUTPUT: «｢Hello｣␤»
  say "__Hello__".match(/__(.*)__/).[0];  # same, in method notation
  ```
  
  Positional and associative subscripting are not mutually exclusive - for example, [`Match`](/type/Match) objects support both (each accessing a different set of data). Also, to make list processing more convenient, class [`Any`](/type/Any) provides a fallback implementation for positional subscripts which simply treats the invocant as a list of one element. (But there's no such fallback for associative subscripts, so they throw a runtime error when applied to an object that does not implement support for them.)
  
  ```raku
  say 42[0];    # OUTPUT: «42␤»
  say 42<foo>;  # ERROR: Type Int does not support associative indexing.
  
  ```
  
  # [Nonexistent elements](#Subscripts "go to top of document")[§](#Nonexistent_elements "direct link")
  
  What happens when a *nonexistent* element is addressed by a subscript is up to the collection type in question. Standard [`Array`](/type/Array) and [`Hash`](/type/Hash) collections return the type object of their [value type constraint](/routine/of) (which, by default, is [`Any`](/type/Any)) unless the collection has been declared with the [`is default`](/type/Variable#trait_is_default) trait in which case the returned value will be whatever the programmer declared it to be:
  
  ```raku
  # no default values specified
  my @array1;     say @array1[10];                   # OUTPUT: «(Any)␤»
  my Int @array2; say @array2[10];                   # OUTPUT: «(Int)␤»
  
  my %hash1;      say %hash1<foo>;                   # OUTPUT: «(Any)␤»
  my Int %hash2;  say %hash2<foo>;                   # OUTPUT: «(Int)␤»
  
  # default values specified
  my @array3 is default('--');     say @array3[10];  # OUTPUT: «--␤»
  my Int @array4 is default(17);   say @array4[10];  # OUTPUT: «17␤»
  
  my %hash3 is default('Empty');   say %hash3<foo>;  # OUTPUT: «Empty␤»
  my Int %hash4 is default(4711);  say %hash4<foo>;  # OUTPUT: «4711␤»
  ```
  
  However, other types of collections may react differently to subscripts that address nonexistent elements:
  
  ```raku
  say (0, 10, 20)[3];           # OUTPUT: «Nil␤»
  say bag(<a a b b b>)<c>;      # OUTPUT: «0␤»
  say array[uint8].new(1, 2)[2] # OUTPUT: «0␤»
  ```
  
  To silently skip nonexistent elements in a subscripting operation, see [Truncating slices](#Truncating_slices) and the [`:v`](#:v) adverb.
  
  # [From the end](#Subscripts "go to top of document")[§](#From_the_end "direct link")
  
  Positional indices are counted from the start of the collection, but there's also a notation for addressing elements by their position relative to the end: `*-1` refers to the last element, `*-2` to the second-to-last element, and so on. From version 6.d, `*-0` refers to the element just beyond the last one
  
  ```raku
  my @alphabet = 'A' .. 'Z';
  say @alphabet[*-1];  # OUTPUT: «Z␤»
  say @alphabet[*-2];  # OUTPUT: «Y␤»
  say @alphabet[*-3];  # OUTPUT: «X␤»
  @alphabet[*-0] = 'þ';
  say @alphabet;
  #  OUTPUT: «[A B C D E F G H I J K L M N O P Q R S T U V W X Y Z þ]␤»
  ```
  
  **Note**: The asterisk, which is actually a [`Whatever`](/type/Whatever), is important. Passing a bare negative integer (e.g. `@alphabet[-1]`) like you would do in many other programming languages, throws an error in Raku.
  
  What actually happens here, is that an expression like `*-1` declares a code object via [`Whatever`](/type/Whatever)-priming - and the `[ ]` subscript reacts to being given a code object as an index, by calling it with the length of the collection as argument and using the result value as the actual index. In other words, `@alphabet[*-1]` becomes `@alphabet[@alphabet.elems - 1]`.
  
  This means that you can use arbitrary expressions which depend on the size of the collection:
  
  ```raku
  say @array[* div 2];  # select the middlemost element
  say @array[$i % *];   # wrap around a given index ("modular arithmetic")
  say @array[ -> $size { $i % $size } ];  # same as previous
  
  ```
  
  # [Slices](#Subscripts "go to top of document")[§](#Slices "direct link")
  
  When multiple elements of a collection need to be accessed, there's a shortcut to doing multiple separate subscripting operations: Simply specify a *list* of indices/keys in the subscript, to get back a *list* of elements - also called a "slice" - in the same order.
  
  For positional slices, you can mix normal indices with [from-the-end](#From_the_end) ones:
  
  ```raku
  my @alphabet = 'a' .. 'z';
  say @alphabet[15, 4, *-9, 11].raku;  # OUTPUT: «("p", "e", "r", "l")␤»
  ```
  
  In the `*-number` construct above, `*` indicates the end of the array as explained above in the [From the end](#From_the_end) section. So if you want to take the last N elements of the array, you will have to create a [`Range`](/type/Range) that includes it.
  
  ```raku
  (5802..5830).map( {.chr} )[*-10..*-5] # OUTPUT:  «(ᚽ ᚾ ᚿ ᛀ ᛁ ᛂ)␤»
  ```
  
  Using `*` as the last element of the range will, effectively, return the last elements of the collection.
  
  For associative slices, the angle brackets form often comes in handy:
  
  ```raku
  my %color = kiwi => "green", banana => "yellow", cherry => "red";
  say %color{"cherry", "kiwi"};  # OUTPUT: «(red green)␤»
  say %color<cherry kiwi>;       # OUTPUT: «(red green)␤»
  say %color{*};                 # OUTPUT: «(red yellow green)␤»
  ```
  
  Be aware that slices are controlled by the *type* of what is passed to ([one dimension of](#Multiple_dimensions)) the subscript, not its length. In particular the *type* can be any of the following:
  
  * a lazy Iterable, that [truncates](#Truncating_slices) in [ ]
  * accordingly, an infinite Range will truncate, but a finite one produces a normal slice
  * '\*' (whatever-star), that returns the full slice (as if all keys/indices were specified)
  * '\*\*' (hyperwhatever-star), that returns the full slice (as if all keys/indices were specified), with any lists flattened recursively (since Rakudo Release 2024.08)
  * any other object, that provides a single-element access rather than a slice
  * Callable, whatever is returned by the callable (this can lead to recursion)
  * empty, the full slice known as [Zen slice](#Zen_slices)
  * any iterable different from the above ones, normal slice
  
  The notable difference between `*` and Zen slice (empty) is that the [`Whatever`](/type/Whatever) star will cause full [reification](/language/glossary#Reify) or itemization, while Zen slice won't. Both versions also [de-cont](https://perl6advent.wordpress.com/2017/12/02/perl-6-sigils-variables-and-containers/#decont).
  
  So even a one-element list returns a slice, whereas a bare scalar value doesn't:
  
  ```raku
  say @alphabet[2,];        # OUTPUT: «(c)␤»
  say @alphabet[2,].^name;  # OUTPUT: «List␤»
  
  say @alphabet[2];         # OUTPUT: «c␤»
  say @alphabet[2].^name;   # OUTPUT: «Str␤»
  
  ```
  
  (The angle bracket form for associative subscripts works out because [word quoting](/language/quoting#Word_quoting:_qw) conveniently returns a [`Str`](/type/Str) in case of a single word, but a [`List`](/type/List) in case of multiple words.)
  
  In fact, the list structure of ([the current dimension of](#Multiple_dimensions)) the subscript is preserved across the slice operation (but the kind of Iterable is not – the result is always just lists.)
  
  ```raku
  say @alphabet[0, (1..2, (3,))];       # OUTPUT: «(a ((b c) (d)))␤»
  say @alphabet[0, (1..2, [3,])];       # OUTPUT: «(a ((b c) (d)))␤»
  say @alphabet[flat 0, (1..2, (3,))];  # OUTPUT: «(a b c d)␤»
  say flat @alphabet[0, (1..2, (3,))];  # OUTPUT: «(a b c d)␤»
  
  ```
  
  ## [Truncating slices](#Subscripts "go to top of document")[§](#Truncating_slices "direct link")
  
  Referring to nonexistent elements in a slice subscript causes the output [`List`](/type/List) to contain undefined values (or [whatever else](#Nonexistent_elements) the collection in question chooses to return for nonexistent elements):
  
  ```raku
  my  @letters = <a b c d e f>;
  say @letters[3..7];  # OUTPUT: «(d e f (Any) (Any))␤»
  
  ```
  
  This behavior, while at first glance may seem unintuitive, is desirable in instances when you want to assign a value at an index in which a value does not currently exist.
  
  ```raku
  my  @letters;
  say @letters; # OUTPUT: «[]␤»
  
  @letters[^10] = 'a'..'z';
  say @letters; # OUTPUT: «[a b c d e f g h i j]␤»
  
  ```
  
  If you want the resulting slice to only include existing elements, you can silently skip the non-existent elements using the [`:v`](#:v) adverb.
  
  ```raku
  my  @letters = <a b c d e f>;
  say @letters[3..7]:v;  # OUTPUT: «(d e f)␤»
  
  ```
  
  The behavior when indexing a collection via [lazy](/language/list#Lazy_lists) subscripts is different than when indexing with their eager counterparts. When accessing via a lazy subscript, the resulting slice will be truncated.
  
  ```raku
  say @letters[lazy 3..7]; # OUTPUT: «(d e f)␤»
  say @letters[     3..*]; # OUTPUT: «(d e f)␤»
  
  ```
  
  This behavior exists as a precaution to prevent runaway generation of massive, potentially infinite [`List`](/type/List)s and the out-of-memory issues that occur as a result.
  
  ## [Zen slices](#Subscripts "go to top of document")[§](#Zen_slices "direct link")
  
  If you put the subscript operator behind an object without specifying any indices/keys at all, it simply returns the subscripted object itself. Since it is empty but returns everything, it is known as a *Zen slice*.
  
  Zen slicing is different from passing a [`Whatever`](/type/Whatever)-star (which, like a normal slice, always returns a List of elements no matter the type of the original object) or an empty list (which returns an empty slice):
  
  ```raku
  my %bag := (orange => 1, apple => 3).Bag;
  say %bag<>;    # OUTPUT: «Bag(apple(3) orange)␤»
  say %bag{};    # OUTPUT: «Bag(apple(3) orange)␤»
  say %bag{*};   # OUTPUT: «(1 3)␤»
  say %bag{()};  # OUTPUT: «()␤»
  ```
  
  Zen slicing does not [reify](/language/glossary#Reify) or [cache](/routine/cache) (not even [`Seq`](/type/Seq)s) and merely returns the invocant. It is usually used to [interpolate](/language/quoting#Interpolation:_qq) entire arrays / hashes into strings or to [decont](/language/glossary#decont).
  
  ```raku
  my @words = "cruel", "world";
  say "Hello, @words[]!";  # OUTPUT: «Hello, cruel world!␤»
  
  my $list = <a b c>;
  .say for $list;   # OUTPUT: «(a b c)␤»
  .say for $list<>; # OUTPUT: «a␤b␤c␤»
  .say for $list[]; # OUTPUT: «a␤b␤c␤»
  
  my @fib = 1,1, * + * … *;
  say @fib[];                 # OUTPUT: «[...]␤»
  ```
  
  # [Multiple dimensions](#Subscripts "go to top of document")[§](#Multiple_dimensions "direct link")
  
  Dimensions in subscripts are separated by a semicolon, allowing to mix lists of elements and dimensions.
  
  ```raku
  my @twodim = (<a b c>, (1, 2, 3));
  say @twodim;
  # OUTPUT: «[(a b c) (1 2 3)]␤»
  
  say @twodim[0,1;1]; # 2nd element of both lists
  # OUTPUT: «(b 2)␤»
  
  my %pantheon = %('Baldr' => 'consort' => 'Nanna' ,
                   'Bragi' => 'consort' => 'Iðunn' ,
                   'Nótt'  => 'consort' => 'Dellingr' );
  say %pantheon{'Bragi','Nótt';'consort'}; # 'consort' value for both keys
  # OUTPUT: «(Iðunn Dellingr)␤»
  ```
  
  Multidimensional subscripts can be used to flatten nested lists when combined with [`Whatever`](/type/Whatever).
  
  ```raku
  my @toomany = [[<a b>], [1, 2]];
  say @toomany;
  # OUTPUT: «[[a b] [1 2]]␤»
  
  say @toomany[*;*];
  # OUTPUT: «(a b 1 2)␤»
  ```
  
  You can use as many *flattening semicolons* as you want; there will be, at most, as many nesting levels flattened as the number of semicolons:
  
  ```raku
  say [[1,2,[3,4]],[4,5]][*;*];     # OUTPUT: «(1 2 [3 4] 4 5)␤»
  say [[1,2,[3,4]],[4,5]][*;*;*;*]; # OUTPUT: «(1 2 3 4 4 5)␤»
  ```
  
  In the first example, with one [`Whatever`](/type/Whatever) less than the number of levels, the deepest one will not be flattened; in the second case it is, since it's greater than the number of levels.
  
  You can use [`Whatever`](/type/Whatever) to select ranges or "rows" in multidimensional subscripts.
  
  ```raku
  my @a = [[1,2], [3,4]];
  say @a[*;1]; # 2nd element of each sub list
  # OUTPUT: «(2 4)␤»
  my @a = (<1 c 6>, <2 a 4>, <5 b 3>);
  say @a.sort( { $_[1] } ); # sort by 2nd column
  # OUTPUT: «((2 a 4) (5 b 3) (1 c 6))␤»
  ```
  
  # [Modifying elements](#Subscripts "go to top of document")[§](#Modifying_elements "direct link")
  
  # [Autovivification](#Subscripts "go to top of document")[§](#Autovivification "direct link")
  
  Subscripts participate in "autovivification", i.e. the process by which arrays and hashes automatically spring into existence when needed, so that you can build nested data structures without having to pre-declare the collection type at each level:
  
  ```raku
  my $beatles;
  
  $beatles{"White Album"}[0] = "Back in the U.S.S.R.";  # autovivification!
  
  say $beatles.raku;  # OUTPUT: «${"White Album" => $["Back in the U.S.S.R."]}␤»
  
  ```
  
  `$beatles` started out undefined, but became a [`Hash`](/type/Hash) object because it was subscripted with `{ }` in the assignment. Similarly, `$beatles{"White Album"}` became an [`Array`](/type/Array) object due to being subscripted with `[ ]` in the assignment.
  
  Note that the subscripting itself does not cause autovivification: It only happens when the result of the subscripting chain is *assigned* to (or otherwise mutated).
  
  # [Binding](#Subscripts "go to top of document")[§](#Binding "direct link")
  
  A subscripting expression may also be used as the left-hand-side of a binding statement. If supported by the subscripted collection's type, this replaces whatever value container would be naturally found at that "slot" of the collection, with the specified container.
  
  The built-in [`Array`](/type/Array) and [`Hash`](/type/Hash) types support this in order to allow building complex linked data structures:
  
  ```raku
  my @a = 10, 11, 12, 13;
  my $x = 1;
  
  @a[2] := $x;  # Bound! (@a[2] and $x refer to the same container now.)
  
  $x++; @a[2]++;
  
  say @a;  # OUTPUT: «[10 11 3 13]␤»
  say $x;  # OUTPUT: «3␤»
  ```
  
  This can be specially useful when lazy data structures are part of a bigger one.
  
  ```raku
  my @fib = 1,1, * + * … ∞;
  my @lucas = 1,3, * + * … ∞;
  my %sequences;
  %sequences<f> := @fib;
  %sequences<l> := @lucas;
  for %sequences.keys -> $s {
      for ^10 -> $n {
          say %sequences{$s}[100+$n*10]/%sequences{$s}[101+$n*10];
      }
  }
  # OUTPUT: 0.6180339887498949 times 20.
  
  ```
  
  In this case, hash keys are bound to lazily generated sequences. The fact that they are bound means that whatever state has been computed is shared by the hash value and the sequence it's bound to, making computations of subsequent elements faster.
  
  See [method `BIND-POS`](#method_BIND-POS) and [method `BIND-KEY`](#method_BIND-KEY) for the underlying mechanism.
  
  # [Adverbs](#Subscripts "go to top of document")[§](#Adverbs "direct link")
  
  The return value and possible side-effect of a subscripting operation can be controlled using adverbs; these are defined on the relevant subscript [operators](/language/operators#Method_postfix_precedence).
  
  Beware of the relatively loose precedence of operator adverbs, which may require you to add parentheses in compound expressions:
  
  ```raku
  if $foo || %hash<key>:exists { ... }    # WRONG, tries to adverb the || op
  
  ```
  
  ```raku
  if $foo || (%hash<key>:exists) { ... }  # correct
  if $foo or %hash<key>:exists { ... }    # also correct
  
  ```
  
  The supported adverbs are:
  
  ## [`:exists`](#Subscripts "go to top of document")[§](#:exists "direct link")
  
  Returns whether or not the requested element exists, instead of returning the element's actual value. This can be used to distinguish between elements with an undefined value, and elements that aren't part of the collection at all:
  
  ```raku
  my @foo = Any, 10;
  say @foo[0].defined;    # OUTPUT: «False␤»
  say @foo[0]:exists;     # OUTPUT: «True␤»
  say @foo[2]:exists;     # OUTPUT: «False␤»
  say @foo[0, 2]:exists;  # OUTPUT: «(True False)␤»
  
  my %fruit = apple => Any, orange => 10;
  say %fruit<apple>.defined;       # OUTPUT: «False␤»
  say %fruit<apple>:exists;        # OUTPUT: «True␤»
  say %fruit<banana>:exists;       # OUTPUT: «False␤»
  say %fruit<apple banana>:exists; # OUTPUT: «(True False)␤»
  ```
  
  May also be negated to test for non-existence:
  
  ```raku
  say %fruit<apple banana>:!exists; # OUTPUT: «(False True)␤»
  
  ```
  
  To check if *all* elements of a slice exist, use an [all](/routine/all) junction:
  
  ```raku
  if all %fruit<apple orange banana>:exists { ... }
  
  ```
  
  It can be used on multi-dimensional arrays and hashes:
  
  ```raku
  my @multi-dim = 1, [2, 3, [4, 5]];
  say @multi-dim[1;2;0]:exists;                # OUTPUT: «True␤»
  say @multi-dim[1;2;5]:exists;                # OUTPUT: «False␤»
  
  my %multi-dim = 1 => { foo => { 3 => 42 } };
  say %multi-dim{1;'foo';3}:exists;            # OUTPUT: «True␤»
  say %multi-dim{1;'bar';3}:exists;            # OUTPUT: «False␤»
  ```
  
  `:exists` can be combined with the [`:delete`](#:delete) and [`:p`](#:p)/[`:kv`](#:kv) adverbs - in which case the behavior is determined by those adverbs, except that any returned element *value* is replaced with the corresponding [`Bool`](/type/Bool) indicating element *existence*.
  
  See [method `EXISTS-POS`](#method_EXISTS-POS) and [method `EXISTS-KEY`](#method_EXISTS-KEY) for the underlying mechanism.
  
  ## [`:delete`](#Subscripts "go to top of document")[§](#:delete "direct link")
  
  Delete the element from the collection or, if supported by the collection, creates a hole at the given index, in addition to returning its value.
  
  ```raku
  my @tens = 0, 10, 20, 30;
  say @tens[3]:delete;     # OUTPUT: «30␤»
  say @tens;               # OUTPUT: «[0 10 20]␤»
  
  my %fruit = apple => 5, orange => 10, banana => 4, peach => 17;
  say %fruit<apple>:delete;         # OUTPUT: «5␤»
  say %fruit<peach orange>:delete;  # OUTPUT: «(17 10)␤»
  say %fruit;                       # OUTPUT: «{banana => 4}␤»
  ```
  
  Note that assigning [`Nil`](/type/Nil) will revert the container at the given index to its default value. It will not create a hole. The created holes can be tested for with `:exists` but iteration will not skip them and produce undefined values instead.
  
  ```raku
  my @a = 1, 2, 3;
  @a[1]:delete;
  say @a[1]:exists;
  # OUTPUT: «False␤»
  .say for @a;
  # OUTPUT: «1␤(Any)␤3␤»
  ```
  
  With the negated form of the adverb, the element is not actually deleted. This means you can pass a flag to make it conditional:
  
  ```raku
  say %fruit<apple> :delete($flag);  # deletes the element only if $flag is
                                     # true, but always returns the value.
  
  ```
  
  It can be combined with the [`:exists`](#:exists) and [`:p`](#:p)/[`:kv`](#:kv)/[`:k`](#:k)/[`:v`](#:v) adverbs - in which case the return value will be determined by those adverbs, but the element will at the same time also be deleted.
  
  See [method `DELETE-POS`](#method_DELETE-POS) and [method `DELETE-KEY`](#method_DELETE-KEY) for the underlying mechanism.
  
  You can use also these adverbs on associative type objects, but it will actually do nothing; it will also return [`Nil`](/type/Nil)
  
  ```raku
  say Hash<foo>:delete; # OUTPUT: «Nil␤»
  
  ```
  
  The adverb can be used on lazy arrays too:
  
  ```raku
  my @lazy-array = lazy 1, 11, 121 ... 10**100;
  @lazy-array[2**24]:delete;
  
  ```
  
  ## [`:p`](#Subscripts "go to top of document")[§](#:p "direct link")
  
  Return both the index/key and the value of the element, in the form of a [`Pair`](/type/Pair), and silently skip nonexistent elements:
  
  ```raku
  my  @tens = 0, 10, 20, 30;
  say @tens[1]:p;        # OUTPUT: «1 => 10␤»
  say @tens[0, 4, 2]:p;  # OUTPUT: «(0 => 0 2 => 20)␤»
  
  my  %month = Jan => 1, Feb => 2, Mar => 3;
  say %month<Feb>:p;          # OUTPUT: «Feb => 2␤»
  say %month<Jan Foo Mar>:p;  # OUTPUT: «(Jan => 1 Mar => 3)␤»
  ```
  
  If you *don't* want to skip nonexistent elements, use the negated form:
  
  ```raku
  say %month<Jan Foo Mar>:!p;  # OUTPUT: «(Jan => 1 Foo => (Any) Mar => 3)␤»
  
  ```
  
  It can be combined with the [`:exists`](#:exists) and [`:delete`](#:delete) adverbs.
  
  See also the [pairs](/routine/pairs) routine.
  
  ## [`:kv`](#Subscripts "go to top of document")[§](#:kv "direct link")
  
  Return both the index/key and the value of the element, in the form of a [`List`](/type/List), and silently skip nonexistent elements. When used on a [slice](#Slices), the return value is a single flat list of interleaved keys and values:
  
  ```raku
  my  @tens = 0, 10, 20, 30;
  say @tens[1]:kv;        # OUTPUT: «(1 10)␤»
  say @tens[0, 4, 2]:kv;  # OUTPUT: «(0 0 2 20)␤»
  
  my  %month = Jan => 1, Feb => 2, Mar => 3;
  say %month<Feb>:kv;          # OUTPUT: «(Feb 2)␤»
  say %month<Jan Foo Mar>:kv;  # OUTPUT: «(Jan 1 Mar 3)␤»
  ```
  
  If you *don't* want to skip nonexistent elements, use the negated form:
  
  ```raku
  say %month<Jan Foo Mar>:!kv;  # OUTPUT: «(Jan 1 Foo (Any) Mar 3)␤»
  
  ```
  
  This adverb is commonly used to iterate over slices:
  
  ```raku
  for %month<Feb Mar>:kv -> $month, $i {
      say "$month had {Date.new(2015, $i, 1).days-in-month} days in 2015"
  }
  
  ```
  
  It can be combined with the [`:exists`](#:exists) and [`:delete`](#:delete) adverbs.
  
  See also the [kv](/routine/kv) routine.
  
  ## [`:k`](#Subscripts "go to top of document")[§](#:k "direct link")
  
  Return only the index/key of the element, rather than its value, and silently skip nonexistent elements:
  
  ```raku
  my @tens = 0, 10, 20, 30;
  say @tens[1]:k;        # OUTPUT: «1␤»
  say @tens[0, 4, 2]:k;  # OUTPUT: «(0 2)␤»
  
  my %month