From the course: Perl 5 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Iterating with foreach

Iterating with foreach - Perl Tutorial

From the course: Perl 5 Essential Training

Start my 1-month free trial

Iterating with foreach

- [Voiceover] The second more common form of the forloop is called foreach. Here's a working copy of foreach.pl from chapter six of the exercise files, and you notice we have the same array that we used in the last couple of lessons. It's an array of five strings built with quote word operator. And here we have a simple foreach loop, and when I run it, you see our result is to display the five strings. The foreach keyword is actually an alias for for, so if I take that and just change it to for like this, and if I save and run, you see I get exactly the same result. Because this may lead to confusion, for clarity's sake, I strongly suggest that you settle on a consistent usage and stick to it. I always use foreach for the iterative loop, as seen here, and for for the traditional C-style for loop, as I showed in the previous video. So this form of the forloop, this foreach loop, iterates over a list or an array. The first argument is a scalar variable, and you see it here, my $s, so…

Contents