Perl Programming - Working with strings
Get the entire Perl Programming course for 20% off: http://stoneriverelearning.com/courses/perl-programming-for-beginners-online-course?product_id=38639&coupon_code=YOUTUBE20
Welcome back, guys! In this lecture, we’re going to be working with strings. So what are strings? Strings are just a group of human-readable characters enclosed by single or double quote marks. Now, strings may contain alphabetic letters, numbers, and punctuations. Single-quoted string literals. A single-quoted string literal is just again a group of characters within single quote marks. The single quote marks are not part of the string itself. Their job is just to let Perl know where the string starts and where it ends. Any character between the single quote marks besides a single quote mark or backslash just represents itself. To use a single quote, use the backslash followed by the single quote. To use a backslash, put two backslashes to use just one backslash. I’ll show you guys how to do this with our examples as well.
So let’s talk about double-quoted string literals. Again, it’s just a group of characters enclosed this time by double quotes. You may use the special backlash escape character to extend its full power of non-printable characters. That’s the benefit of using double-quote marks. It uses escape characters to print of course non-printable characters which I’ll show you as well.
Some of the often-used escape characters. To create a new line after a string, it’s \n. To create a tab space, it’s \t. Of course to create a single backslash, you have to put \\ because the single backslash lets Perl know that we’re going to be using a special escape character and within our string. If you want to use a double quotation mark inside the string, you put two additional ones. So it will be double quotation mark double quotation mark.
So string operators. Let’s talk about the dot operator or I’ll look at it as the concatenation operator. So we can actually combine strings. Strings can be joined together with a dot operator or also known as a period character. This does not modify the current string.
Let’s jump into examples. Here we are. We’re going to do some live examples. Let’s start with the single quote marks. Let’s declare a variable. We start with the dollar sign. Let’s just call it String 01 followed by an assignment operator to give it a value. Let’s just call this ‘This is value 1.’ We’ll just copy this line of code and paste it to make another string but we’re going to call this one String 02. For this value, we’re going to use double quotes. I’ll put double quotes. value 2.
If we print String 01, let’s see what happens. I’ll copy String 01 and paste it in here. Save our work. Go to Run. Run Script. Nothing fancy. It just printed our value. This is Value 1. Press any key to continue. Now let’s look at String 02. If we just replace the 01 with 02, let’s see what happens here. We go to Run. Run Script. Pretty much it did the same thing, correct, as we see. So now let’s use some power with the double quotation marks. If we use our special escape character and we want to add a new line to our string, remember it’s backslash to let Perl know that we’re going to be using an escape character and the new line character. We save it. Click Run. Run Script. There we have it. So now we were able to create a new line and as we can see Press any key to continue is on a new line by itself. Of course we can do a tab as well so we use \t just to let you guys see what it looks like. Let me see here. Click Run Script. As we can see, it gave us an invisible tab. Press any key to continue instead of our new line character. So that’s the power of using double quotation marks. We can use non-printable characters which is very nice because we can format text.
Let’s use the string concatenation or our dot operator. So if we take String 01 and we use the dot for combining strings, we take String 02. Save our work. Click Run. Run Script. Look what happens. This is value 1. This is value 2. We still have the tab here as well on our String 02. So let’s remove the t and put an n. We can actually add a space. So let’s add a space. Let’s just use double quotation marks and add a space. After the double quotation marks, again we’re going to use another dot operator to combine all three strings. Let’s see what happens now. Go to Run. Run Script. There we have it. This is value 1 with a space. This is value 2 with a new line. That’s being created with Press any key to continue by itself.
Again, if we want to just use a let’s say… I’m going to put my name, Victors. Of course if we do this, we run that. It’s going to give us a bunch of errors. Why? Because we want to escape and use this literal character which is a single quote. So we have to use the backslash. That’s to let Perl know we want to use that character. Run the script.