Web www.tizag.com

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 /gr44";
print $double."<br /gr44";
print $userdefined."<br /gr44";

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 allows for the temporary replacement of characters in a string. We can change the string "Hello, PERL" to "Hello, World!" quite easily. Each character of the string is automatically assigned a numeric value by PERL, which means that we can index any of the characters in our strings with this number. PERL counts each character of the string beginning with 0 for the first character and continuing until it reaches the end of a string.

Two arguments must be sent with our substr() function, the string you wish to index and the index number. If two arguments are sent, PERL assumes that you are replacing every character from that index number to the end.

stringreplace.pl:

#!/usr/bin/perl

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

# DEFINE A STRING TO REPLACE
$mystring = "Hello, PERL!";

# PRINT THE BEGINNING STRING
print $mystring."<br />";

# RUN OUR FUNCTION AND CHANGE OUR STRING
substr($mystring, 7) = "World!";

# PRINT THE NEW STRING
print $mystring;

Because we only specified one numeric parameter for the string, PERL assumed we wanted to replace every character after the 7th, with our new string. If we throw a third parameter in our function we can replace only a chunk of our string with a new string.

stringreplace.pl:

#!/usr/bin/perl

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

# DEFINE A STRING TO REPLACE
$mystring = "Hello, PERL!";

# PRINT THE BEGINNING STRING
print $mystring."<br />";

# RUN OUR FUNCTION AND CHANGE OUR STRING
substr($mystring, 7, 11) = "World";

# PRINT THE NEW STRING
print $mystring;

We have essentially created the same script as in the previous example, the difference is that we replaced ONLY the four letters of PERL with World, the exclamation point at the end was ignored.





New - Tizag.com Forums!
Recent Forum Topics: