Loop over multiple arrays simultaneously: Difference between revisions

Content added Content deleted
(→‎{{header|Raku}}: add a roundrobin example)
(→‎{{header|Raku}}: add verbiage, show longhand operation)
Line 2,840: Line 2,840:
{{works with|rakudo|2015.12}}
{{works with|rakudo|2015.12}}


Note that all of the following work with ''any'' iterable object, (array, list, range, sequence; anything that does the Iterable role), not just arrays.

=== Basic functionality ===
<lang perl6>for <a b c> Z <A B C> Z 1, 2, 3 -> ($x, $y, $z) {
<lang perl6>for <a b c> Z <A B C> Z 1, 2, 3 -> ($x, $y, $z) {
say $x, $y, $z;
say $x, $y, $z;
Line 2,856: Line 2,859:
We could also use the zip-to-string with the reduction metaoperator:
We could also use the zip-to-string with the reduction metaoperator:


<lang perl6>.say for [Z~] [<a b c>], [<A B C>], [1,2,3]</lang>
<lang perl6>.say for [Z~] <a b c>, <A B C>, (1,2,3);</lang>

We could also write that out "long-hand":

<lang perl6>.say for zip :with(&infix:<~>), <a b c>, <A B C>, (1,2,3);</lang>

returns the exact same result so if you aren't comfortable with the concise operators, you have a choice.


=== A list and its indices ===
=== A list and its indices ===