subscript adverb :delete

Documentation for subscript adverb :delete, assembled from the following types:

language documentation Subscripts

From Subscripts

(Subscripts) subscript adverb :delete

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.

my @tens = 0102030;
say @tens[3]:delete;     # OUTPUT: «30␤» 
say @tens;               # OUTPUT: «[0 10 20]␤» 
my %fruit = apple => 5orange => 10banana => 4peach => 17;
say %fruit<apple>:delete;         # OUTPUT: «5␤» 
say %fruit<peach orange>:delete;  # OUTPUT: «(17 10)␤» 
say %fruit;                       # OUTPUT: «{banana => 4}␤» 

Note that assigning 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.

my @a = 123;
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:

say %fruit<apple> :delete($flag);  # deletes the element only if $flag is 
                                   # true, but always returns the value. 

Can be combined with the :exists and :p/:kv/:k/: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 and method DELETE-KEY for the underlying mechanism.