Course of Raku / Advanced / Containers / Contexts 🆕 / Item and list context

Quiz — Item context

What does the following program print?

my @a = 1, 2, 3;
my $n = 0;
$n++ for $(@a);
say $n;
03
11
00
06

The $( … ) contextualiser forces item context, so the array is treated as a single value rather than a list of elements. The for loop therefore sees one item and runs its body just once, leaving $n at 1. Without $( ), the loop would iterate three times.

Course navigation

Item and list context   |   Force a number