O'Reilly logo

Sams Teach Yourself Perl in 21 Days, Second Edition by Rafe Colburn, Laura Lemay

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Iteration with foreach and for Loops

while loops provide a general way to repeat a block of code—that block will just keep executing until some test is met. The for loop, a second form of loop, is a slightly different approach to the same problem. With for loops, the loop is executed a specific number of times, and then stops. This is sometimes called iteration because the focus is on the specific number of loops as opposed to the more vague “loop until true” that the while provides.

In just about every case, you could write a while loop to do iteration, or you could write a for loop to do the same thing as a while. But some tasks lend themselves better to one form or another, and so we have multiple kinds of loops.

Perl provides two for loops: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required