September 2017
Beginner
402 pages
9h 52m
English
The execution of the loop can be controlled not only with initial conditions but also from the loop body itself. There are three keywords for that—next, last, and redo. They become more useful when used together with condition checks using if.
The last keyword just breaks the loop. Let's consider an example of a loop that breaks after it prints a value that is bigger than some predefined threshold:
my @data = 3, 1, 7, 12, 50, 2, 14;for @data -> $x { last if $x > 42; say $x;}say 'Done.';
Notice that if is used in the postfix form here—it behaves the same as the following code:
if $x > 42 { last;}The first few iterations are executed as usual, but when the current value of $x becomes 50, which makes the $x > 42 condition ...
Read now
Unlock full access