subscript adverb :kv

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

language documentation Subscripts

From Subscripts

(Subscripts) subscript adverb :kv

Return both the index/key and the value of the element, in the form of a List, and silently skip nonexistent elements. When used on a slice, the return value is a single flat list of interleaved keys and values:

my  @tens = 0102030;
say @tens[1]:kv;        # OUTPUT: «(1 10)␤» 
say @tens[042]:kv;  # OUTPUT: «(0 0 2 20)␤» 
 
my  %month = Jan => 1Feb => 2Mar => 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:

say %month<Jan Foo Mar>:!kv;  # OUTPUT: «(Jan 1 Foo (Any) Mar 3)␤» 

This adverb is commonly used to iterate over slices:

for %month<Feb Mar>:kv -> $month$i {
    say "$month had {Date.new(2015$i1).days-in-month} days in 2015"
}

Can be combined with the :exists and :delete adverbs.

See also the kv routine.