Jonathan's Corner
(Search & Sitemap)
> Writing >
Miscellaneous Nonfiction >
Tinkering with Perl
Skip Back
Previous
38
39
40
41
42
43
44
45
46
47
Next
Skip Forward
Printer-Friendly Version
A for loop is a special case of a while loop, which is commonly used for doing something a certain number of times.
Let's suppose that I wanted to print "Hello, world!" ten times. I could just have ten lines like so:
print "Hello, world!\n"; print "Hello, world!\n"; print "Hello, world!\n"; print "Hello, world!\n"; print "Hello, world!\n"; print "Hello, world!\n"; print "Hello, world!\n"; print "Hello, world!\n";
But what if I wanted to change the number -- to five or to fifty? It would take a lot of editing.
There's a better way to do it, though. I could have the computer count up to ten, and each time print out "Hello, world!". If I did that, then changing it to five or fifty would only mean changing the number I have to count to.
Let me give some code to do that, and explain what it does.
for($CurrentLine = 0; $CurrentLine < 10; ++$CurrentLine)
{
print "Hello, world!\n";
}
That's all the code that it takes. And what if I wanted to print "Hello, world!" a thousand times? A very easy change -- only change the 10 to 1000:
for($CurrentLine = 0; $CurrentLine < 1000; ++$CurrentLine)
{
print "Hello, world!\n";
}
Now, let me explain what it does. Let's look at the first line. It has the following format:
for(part one; part two; part three)
It has three parts, separated by semicolons (';'). Each part does something different.
The first part is run exactly once. It is used to set things up -- in this case, to assign the scalar variable the value of zero.
For reasons that you will understand later when you program, you should have computers start counting at 0. So, instead of counting from 1 to 10, this counts from 0 to 9 -- and still does it ten times.
The second part is the conditional clause that is tested each time you run through the loop. In this case, it makes an arithmetic assertion: that CurrentLine is less than 10. After running through the loop, the computer checks to see if the second part is still true -- if it's true, the computer runs through the loop one more time.
The third part is something the computer does each time, after running through the loop and before checking the conditional clause. In this case, it increments the value of $CurrentLine.
So, all together, we have the computer counting from 0 to 9, and each time printing out a "Hello world!" message.
Tinkering with Perl is a free book that provides an introduction to programming in Perl, as well as a basic reference for things like foreach in Perl, if-then, and if-then-else, in addition to providing a glossary where you can find definitions for concatenate and other terms.
Tinkering with Perl may be one of the most popular offerings on this site, but it's not the only attraction. You can read a tongue-in-cheek Game Review: Meatspace, read an even more offbeat customer service survey (whether or not you actually fill it out), and spend a few minutes wishing your boss would read, The Administrator Who Cried, "Important!" (Not to mention that there are other things you can read here besides tech stuff, from Janra Ball: The Headache to The Spectacles.)
Jonathan's Corner
(Search & Sitemap)
> Writing >
Miscellaneous Nonfiction >
Tinkering with Perl
Skip Back
Previous
38
39
40
41
42
43
44
45
46
47
Next
Skip Forward
Printer-Friendly Version