3
# This file assumes that you already read (and understood) earlier sessions!
8
# Let's say you want to count from 1 to 10. Pretty easy, just write:
29
print "$counter - done!\n";
31
# Works perfectly, but there are some drawbacks:
32
# - It takes 21 lines (imagine you want to count until 100!)
33
# - What happens if you need to change $counter to $counter2?
35
print "--- next sample ---\n";
37
# Every time you copy/paste a line of sourcecode, stop a moment and think
38
# about looping. This means, let Perl repeat some lines for you:
43
# "for" is a special keyword which says Perl: "I'll give you a list of items
44
# and for each item, please execute the following sourcecode once."
45
# Sounds much more complicated than it acutally is.
46
# (1..10) is a range: It has a starting number (1) and a final value (10)
47
# and Perl should give you all numbers from 1 to 10.
48
# The lines which belong to this "for" - loop are between the same brackets { }
49
# you already know from if. Actually every logical block of sourcecode is
52
# If you write more than one line of sourcecode between { and }, write this
53
# sourcecode as seperate lines. Perl dosn't care, but you and others need to
54
# be able to read it without scrolling thousend columns to the right.
56
# It might look like a waste of time, but if you put spaces or tabs before each
57
# line within a if/loop/block, your source will be much more readable. Start
58
# now and you're used to do it in a few days and it will save you very much
59
# time hunting for lines in the wrong block lateron. Perl masters always do it.
62
print "$loop_counter\n";
66
# If you ignore my big boring comments above, this loop does the same in only 5
67
# lines what we did earlier in 21 lines and you got three places left where you
68
# need to change the variable name if you're forced to.
70
print "--- next sample ---\n";
72
# Perl could also be used for cooking:
74
for my $fruit ( "Orange", "Apple", "Strawberry", "Melon", "Lemon" ) {
77
print "Cut the fruits in not-too-small pieces and your fruitsalat is done.\n";
79
# If you add a variable just behind "for", this variable will have the item for
80
# the current loop run. The salat example just prints the $Fruit, but you could
81
# do much more inside the loop.
83
print "--- next sample ---\n";
85
# "for" - loops got one drawback: You need to know how many times your loop
86
# should run before the loop starts and sometimes you don't know this.
88
# This sample tries to find out how often a given number (for example 123)
89
# could be divided by 2 before it gets lower than two.
94
# Two variables, one holding the number and the second for the number of times
95
# we divided the number.
97
while ( $number > 2 ) {
99
# while loops run until the condition (which is the same we used for the if's)
100
# is no longer true. $number is 123 at the first run which is greater than 2
101
# and the following lines are executed.
104
$number /= 2; # Look at the math session to understand this.
108
print "Divided $two_count times\n";
110
# Always beware of endless loops where the condition always stays true! Your
111
# program would never leave the loop.
113
# Now press F5 and Padre will execute this script.
115
# You'll see a new window on the bottom of Padre which shows you the
116
# output of this script.
117
# We have a lot of samples with a lot of output in this file, so first try to
118
# match each block of output to the correct source code sample and try to
119
# understand what happens. Play around with the source if you want and change
120
# things to get other results.
122
# If you understood this lesson, this is easy for you:
123
# Try to write a loop which shows all even numbers from 20 to 30.
124
# Hint: Remember that 10 * 2 is 20 and 11 * 2 is 22.
126
# Got it working? Congratulations!
127
# Next, try to do the same using a while loop instead of for.