Course of Raku / Advanced / Containers / Subscript adverbs 🆕 / Exercises / Does the key exist

Solution: Does the key exist

Here is a possible solution to the task.

Code

my %h = a => 1, b => 2;
say %h<z>:exists;

🦋 You can find the source code in the file check-exists.raku.

Output

False

Comments

  1. The :exists adverb returns whether the key is present, without fetching the value.

  2. The hash has no key z, so it returns False — and, unlike an ordinary lookup, it does not create the key as a side effect.

Course navigation

Does the key exist   |   Delete a key