Loops/Foreach: Difference between revisions

Content added Content deleted
Line 1,409: Line 1,409:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|Rakudo|#21 "Seattle"}}
{{works with|Rakudo|2015.10-40}}
<lang perl6>say $_ for @collection;</lang>
<lang perl6>say $_ for @collection;</lang>
Perl 6 leaves off the <tt>each</tt> from <tt>foreach</tt>, leaving us with <tt>for</tt> instead. The variable <tt>$_</tt> refers to the current element, unless you assign a name to it using <tt>-></tt>.
Perl 6 leaves off the <tt>each</tt> from <tt>foreach</tt>, leaving us with <tt>for</tt> instead. The variable <tt>$_</tt> refers to the current element, unless you assign a name to it using <tt>-></tt>.
<lang perl6>for @collection -> $currentElement { say $currentElement; }</lang>
<lang perl6>for @collection -> $currentElement { say $currentElement; }</lang>
Perl 6 will do it's best to put the topic at the right spot.
<lang perl6>.say for @collection;
for @collection { .say };</lang>
Iteration can also be done with hyperoperators. In this case it's a candidate for autothreading and as such, execution order may vary. The resulting list will be in order.
<lang per6>@collection>>.say;
@collection>>.=&infix:<+>(2); # increment each element by 2</lang>


=={{header|Phix}}==
=={{header|Phix}}==