|
|
The print Command |
|
| Before we go any further you should know how to use the print command. | |
| The print command writes text to your prompt screen. You would use it by typing print "" and putting what ever you want to output in the quotes. | |
|
You can display the value of the scalar variable $snerble by entering print "$snerble"; This may seem a little weird for those of you who have programmed before, but perl does like it variables inside of the quotes. |
The while Loop | |
| The while loop allows the programmer to run a certain amount of code while a certain condition is true. | |
| An analogy to the real world would be: While I am programming I pretend to think. | |
| So, I would continually pretend to think as long as I am programming. | |
|
If you wanted the program to run a loop until it counted to 20 you would type: $varname=1; while($varname<=20) { print "$varname"; $varname++; } |
|
| The line with $varname++; increases the value of $varname to one above what it used to be. | |
The For Loop | |
| The for loop allows us to make a certain amount of code run a certain number of times while changing the value of a variable with each loop. | |
| ...okay, mabey you should read that above sentence a few times slowly. | |
| A good example of the for loop is one that counts up to 100. | |
Example: |
|
|
for($i = 0; $i <= 100; $i++) { print "\nThis is line $i, enjoy!"; } |
|
The foreach Loop | |
| This last loop is a lazy way of changing or getting the values from an entire array. | |
| For each element in an array this loop executes a certain amount of code. | |
Example | |
|
@turtle = (4.5,4.2,"narf","zonk","5th element"); foreach $glerb (@turtle) { print("gerbil $glerb "); } |
|
|
|