Looping over any number of elements
Iterating over more than one array in parallel
Missing valuesOne should ask the question what happens if the list runs out of values in the middle, of a multi-value iteration? That is, what happens to the follow loop? examples/arrays/missing_values.p6#!/usr/bin/perl6
use v6;
for (1, 2, 3, 4, 5) -> $x, $y {
say "$x $y";
}
In this case Rakudo throws an exception when it finds out it does not have enough values for the last iteration. It will look like this, (with a bunch of trace information afterwards). 1 2 3 4 StopIteration In order to avoid the exception we could tell the loop that the second and subsequent values are optional by adding a question mark after the variable examples/arrays/missing_values_fixed.p6#!/usr/bin/perl6
use v6;
for (1, 2, 3, 4, 5) -> $x, $y? {
say "$x $y";
}
Unfortunately as of today this latter construct does not work yet. Copyright 2006, 2007, 2008, 2009, 2010 Gabor Szabo http://szabgab.com/ Index | TOC |
Follow me: