Quantcast
Viewing latest article 4
Browse Latest Browse All 8

Perl With This Loop, I Thee Wed

With This Loop, I Thee Wed

So you know if/else, you know variables, and you know assignments, but I bet you don’t know loops. That’s right, my friend, you’re not there yet. Even the Karate Kid had a few things to learn from Mr. Miagi.

Anyone who has used a computer language should be familiar with the format of loops. If not, don’t worry too much about it, because I’m paid the big bucks to show you how it’s done.

Loops fall under a part of scripting with the apt name “command structure.” Usually loops form the body of the script, which sends out the information and instructions to the rest of the script. Loops repeat again and again and stop only when certain parameters are met. Enter Rule #6.

Rule #6: All loops must end. It sounds simple, and for the most part it is. You just have to keep track of your variables through every step of the loop (revisit Rule #3).

The essential format of a loop looks like this (although they can include if/else statements and even more loops):

command (statement) {



	statements;



}

The basic kinds of loops in Perl are the for loop, the while loop, and the foreach loop.

The For Loop

The for loop is, without a doubt, my favorite loop because it’s elegant and self-contained. I fell in love with it while working with JavaScript and continued the affair through Java. It still burns strong with Perl. Its format is the most complicated of all of the loops, but complexity in computing usually means more power — or that’s what I keep telling myself. Here’s what it looks like:

for ($i = 0; $i <= $#stuff; $i++) {



	print "$stuff[$i]";



}

In the for loop, you define a variable that exists only within the loop. It doesn’t have to be called $i, but it’s a stand-by variable for incremental variables. I defined the variable $i to equal 0. Then I told the loop to keep looping, as $i is less than or equal to the number of slots in the array @stuff. And then I told it to add 1 to $i at the end of each pass through the loop. Next I had the script print the @stuff array slot that $i represented. So on its first pass through the loop, it would print $stuff[0]. On the second pass, it would print $stuff[1]. This pattern would continue until the condition set was no longer valid.

I love these. My vision of the perfect woman is one who can write a killer for loop and will listen to Pixies songs all day long. (Come to think of it, if you’re out there and you can write a for loop to play a Pixies song, send me a note.)

The While Loop

The while loop is just like the for loop, only not as self-contained. A sample for loop would look something like this:

$stuff = <NAMES>;



while ($stuff ne "bob") {



	print "$stuff";



	$stuff = <NAMES>;



}

This code executes the statements inside the loop as long as the variable $stuff doesn’t equal “bob.” This loop runs the greatest risk of never ending, because it doesn’t rely on a value test to stop as the for loop did. Rather, you have to define a variable outside the loop and have the value change somewhere inside. It’s tricky.

Foreach Loop

Foreach loops are kind of cool. They’re like a lazy version of the for loop. Take a look:

foreach $slotnum (@stuff) {



	print "$slotnum";



}

One slot at a time, this loop will take the lowest value and assign it to $slotnum to be used as a wild-card variable later on. So the @stuff array will start with slot number 0 and go all the way to slot 100 (if there is one). Or it’ll stop if it runs out of slots. The foreach loop is really useful for running through associative arrays since their slots aren’t numbered. Check this out:

foreach $slotname (keys (%stuff)) {



	print "$stuff{$slotname}";



}

This code grabs each key value to the %stuff array one value at a time. (Have you already forgot about associative arrays? Go back to Scalers, Arrays, and Associative Arrays, do not pass Go, and do not collect $200.) It does this by using the built-in Perl function keys and then printing out the value of each slot. Don’t worry:We’ll get to printing. In the meantime, wax on, wax off, wax on, wax off.

Righty-o! Now you’re almost ready to Perl, but you still need something to manipulate.


Viewing latest article 4
Browse Latest Browse All 8

Trending Articles