Quantcast
Viewing latest article 5
Browse Latest Browse All 8

Additional Functions

Additional Functions

Great, you’ve almost got it. Just look at you! My little Perl Jedi! Of course, even a Jedi needs a few weapons. Here’s a few extra things to help you write the perfect Perl script to master the Force and all that garbage.

Splitting

Splitting is a function that will save you lots of time and money (well, time, at least). It looks for a pattern in a string and splits up the string into an array based on the pattern. Have a look.

 #!/usr/local/bin/perl

 

 # I have a string.



 $stuff = "Colin is so cool";



 # I decide that I want to split it up by spaces into an array.



 @stuff = split (/ /, $stuff);



 # I then want to print out each slot on a different line.



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



 	print "Slot $i:$stuff[$i]\n";



 }



 # Find out how many words there were like this.



 print "There were $#stuff words.\n";

Play around with this tool a little bit, and you’ll find out just how useful it really is.

Search and Replace

If you always misspell the same words as I do, then you’ll love Search and Replace. Here’s how it works:Place =~ s/searchstring/replacestring/gi; after the string you want to run Search and Replace on, replacing searchstring and replacestring with the items you want searched and replaced, like so:

 $stuff =~ s/bad/bad bad leroy brown/gi;

So if I wanted to fix all my “e”s before “i”s, I might do something like this:

 # I have this string:



 $stuff = "beleive releive greive";



 # I want to fix all the mistakes.



 $stuff =~ s/ei/ie/gi;



 # Then I want to print out the output.



 print "$stuff";

The niftiest part is that you have a couple of different options with this tool. At the end of statement, there are two letters:”g” and “i.” Those are two options:”g” tells the command to search and replace all occurrences of the string, and “i” tells it to ignore case. One other cool option is the “o” option, which only replaces the first occurrence. (It’s the opposite of the “g” option.) You can use one, all, or none of these options with the command.

Quick Search

Last, you can even use it to tell if something is in a string. Say that you wanted to do something only if the string contained the word “cool.” The hard way to do this is to split up the string and run a loop through the array, looking for the word. Thankfully, there’s any easier way:a quick search.

 $stuff =~ /cool/

This statement would return a true value if the string contains the word “cool” and return a false value if it didn’t. You can also do the reverse:

 $stuff != /cool/



… which would return a true if the string doesn’t contain the word “cool” and a false if it does. Then you can use it in an if statement like so:

 if ($stuff =~ /cool/) {



 	statements



 }



All kinds of niftiness!

Now that I’ve given you a taste of Perl, no doubt you’re probably jonesing to actually do something with it. Read on to learn how to turn Perl into CGI.


Viewing latest article 5
Browse Latest Browse All 8

Trending Articles