Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Foreach loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Raku === In [[Raku (programming language)|Raku]], a sister language to Perl, ''for'' must be used to traverse elements of a list (''foreach'' is not allowed). The expression which denotes the collection to loop over is evaluated in list-context, but not flattened by default, and each item of the resulting list is, in turn, aliased to the loop variable(s). List literal example: <syntaxhighlight lang="Perl6"> for 1..4 { .say; } </syntaxhighlight> Array examples: <syntaxhighlight lang="Perl6"> for @arr { .say; } </syntaxhighlight> The for loop in its statement modifier form: <syntaxhighlight lang="Perl6"> .say for @arr; </syntaxhighlight> <syntaxhighlight lang="Perl6"> for @arr -> $x { say $x; } </syntaxhighlight> <syntaxhighlight lang="Perl6"> for @arr -> $x, $y { # more than one item at a time say "$x, $y"; } </syntaxhighlight> Hash example: <syntaxhighlight lang="Perl6"> for keys %hash -> $key { say "$key: $hash{$key}"; } </syntaxhighlight> or <syntaxhighlight lang="Perl6"> for %hash.kv -> $key, $value { say "$key: $value"; } </syntaxhighlight> or <syntaxhighlight lang="Perl6"> for %hash -> $x { say "$x.key(): $x.value()"; # Parentheses needed to inline in double quoted string } </syntaxhighlight> Direct modification of collection members with a doubly pointy block, ''<->'': <syntaxhighlight lang="Perl6"> my @arr = 1,2,3; for @arr <-> $x { $x *= 2; } # Now @arr = 2,4,6; </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)