DavidSzczesniak
1/2/2018 - 5:35 PM

The foreach loop

Steps through a list of values, executing one iteration for each value.

foreach $rock (qw/bedrock slate lava/) {
  print "One rock is $rock.\n"; # Prints names of three rocks, spaced out by newlines
}

\!h # IMPORTANT: this does not create copies of these list elements, it uses them literally. 
\!h # This means any modifications made will stick after the loop.

my $rocks = qw/bedrock slate lava/;
foreach $rock (@rocks) {
  $rock = "\t$rock"; # put a tab in front of each element of @rocks
  $rock = "\n";      # put a newline on the end of each
}

print "The rocks are:\n", @rocks; # Each one is indented, on its own line