subscript adverb :exists

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

language documentation Subscripts

From Subscripts

(Subscripts) subscript adverb :exists

Return 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:

my @foo = Any10;
say @foo[0].defined;    # OUTPUT: «False␤» 
say @foo[0]:exists;     # OUTPUT: «True␤» 
say @foo[2]:exists;     # OUTPUT: «False␤» 
say @foo[02]:exists;  # OUTPUT: «(True False)␤» 
my %fruit = apple => Anyorange => 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:

say %fruit<apple banana>:!exists# OUTPUT: «(False True)␤» 

To check if all elements of a slice exist, use an all junction:

if all %fruit<apple orange banana>:exists { ... }

:exists can be combined with the :delete and :p/:kv adverbs - in which case the behavior is determined by those adverbs, except that any returned element value is replaced with the corresponding Bool indicating element existence.

See method EXISTS-POS and method EXISTS-KEY for the underlying mechanism.