About Me

Friday 20 April 2012

PERL - Strings

PERL - Strings

Strings are scalar as we mentioned previously. There is no limit to the size of the string, any amount of characters, symbols, or words can make up your strings.


When defining a string you may use single or double quotations, you may also define them with the q subfunction.

definestrings.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #HTTP HEADER

# DEFINE SOME STRINGS
$single = 'This string is single quoted';
$double = "This string is double quoted";
$userdefined = q^Carrot is now our quote^;

# PRINT THEM TO THE BROWSER
print $single."<br />";
print $double."<br />";
print $userdefined."<br />";

PERL - Formatting Strings w/ Formatting Characters

Strings can be formatted to your liking using formatting characters. Some of these characters also work to format files created in PERL. Think of these characters as miniature functions.
CharacterDescription
\LTransform all letters to lowercase
\lTransform the next letter to lowercase
\UTransform all letters to uppercase
\uTransform the next letter to uppercase
\nBegin on a new line
\rApplys a carriage return
\tApplys a tab to the string
\fApplys a formfedd to the string
\bBackspace
\aBell
\eEscapes the next character
\0nnCreates Octal formatted numbers
\xnnCreates Hexideciamal formatted numbers
\cXControl characters, x may be any character
\QDo not match the pattern
\EEnds \U, \L, or \Q functions

formattingcharacters.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #HTTP HEADER

# STRINGS TO BE FORMATTED
$mystring = "welcome to tizag.com!"; #String to be formatted
$newline = "welcome to \ntizag.com!";
$capital = "\uwelcome to tizag.com!";
$ALLCAPS = "\Uwelcome to tizag.com!";

# PRINT THE NEWLY FORMATTED STRINGS
print $mystring."<br />";
print $newline."<;br />";
print $capital."<br />";
print $ALLCAPS";
Any combination of these special characters can be used at any time to properly punctuate your strings. They also come in handy when printing out HTML with your PERL functions.

PERL - Substr() and String Indexing

The substr() function is a rather complicated function. It can be used to do many things and we'll start with the most basic, grabbing a substring and move onto more advanced ideas further on.
To use substr() to grab a substring, you need to give it both a string variable to pick something out of and an offset (which starts at 0). A string can be thought of as an array of characters, starting with element 0 at the beginning and +1 for each additional character. The string "hey" has 3 characters. The 0th element is "h", the 1st element is "e" and the 2nd and last element is "y".
The first argument of substr() is the string we want to take something from and the second argument is the offset, or where we want to start at.

stringreplace.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #HTTP HEADER

# DEFINE A STRING TO REPLACE
$mystring = "Hello, am I about to be manipulated?!";

# PRINT THE ORIGINAL STRING
print "Original String: $mystring<br />";

# STORE A SUB STRING OF $mystring, OFFSET OF 7
$substringoffset = substr($mystring, 7);
print "Offset of 7: $substringoffset<br />";

Display:

Original String: Hello, am I about to be manipulated?!
Offset of 7: am I about to be manipulated?!
substr() started at the 7th element (remember we count from 0) which was the "a" in "am" and returned the rest of the string and we stored it into $substringoffset. Play around with this function a little and get a feel for how offset works!
Below we have gone on to the more advanced options of substr(), taking advantage of the last two arguments of the function: length and replace value. Rather than grabbing the whole string from the offset, we can just grab a chunk of it by specifying the length we want this substring to be.
The final argument, replace value, replaces the substring specified by the first three arguments with whatever we want. Let's change the original string to say something different by grabbing a part of the string and replacing it with "I want".

stringreplace.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #HTTP HEADER
# DEFINE A STRING TO REPLACE
$mystring = "Hello, am I about to be manipulated?!";

# PRINT THE ORIGINAL STRING
print "Original String: $mystring<br />";

# STORE A SUB STRING OF $mystring, OFFSET OF 7 AND LENGTH 10
$suboffsetANDlength = substr($mystring, 7, 10);
print "Offset of 7 and length of 10: $suboffsetANDlength<br />";

# CHANGE $mystring, OFFSET OF 7 AND LENGTH 10 AND 
# REPLACE SUB STR WITH "I want"
$suboffsetANDlength = substr($mystring, 7, 10, "I want");
print "mystring is now: $mystring<br />";

Display:

Original String: Hello, am I about to be manipulated?!
Offset of 7 and length of 10: am I about
mystring is now: Hello, I want to be manipulated?!
The original string was changed, so be careful when using the replace value argument of substr(). However, it's a great tool to have in your arsenal, as changing strings in this manner is pretty common. Please play around with substr() for a while and make sure you understand it!

0 comments:

Post a Comment