Perl Programming - Working with the chomp operator
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
Okay, guys. Welcome to our fifth lecture of section 3. Let’s talk about the chomp operator.
The chomp operator is a Perl built-in operator that works on a variable and pretty much a variable that holds a string value. The variable has to hold the string. If the string ends in a new line character, the chomp can get rid of the new line. The chomp operator, the responsibility of this operator is just to remove a new line character from a string value. Once we do some examples, I’ll show you why it’s important for this to happen with the chomp operator. It’s the best way to remove a trailing new line from a string and a variable as well. Again, that’s all the chomp operator does. It just cuts off the new line character from a string value. Let’s go ahead and do some examples.
If I declare a few string values, let me set up some scholar variables. I’ll name this string01 and I’ll just put for the value String 1. Let me just put for the value String 1. Let me just copy and paste this. I’ll just name them string02 and string03. So string 02 and string03, String 2 and String 3. So if we put a new line character on our strings which is the \n and that adds a new line character to our strings, the end of our string that is, let’s see what happens. Let’s say if we just print them out, I’ll go ahead and type string01 so I’ll copy our scalar variable and I’ll just paste it in our double quotes. I’ll put a space and I’ll do the same thing with string02 and then I’ll do the same thing with string03. Let’s see what happens here. So if we just leave it like this, we save our work, Ctrl + S. We press F5. Let’s save our work first. I’ll just save it as sample.pl which is our Perl program. This file already exists. I’ll click Yes to overwrite it and I’ll just click F5. Let’s see what happens. As we can see, we have this nasty-looking output right here. Again, it’s giving us our String 1. It’s creating a new line, so it’s given us a vertical output.
Let’s say before we use those prints, let’s use our chomp operators. So if we do chomp and we do the string01, so let’s just copy and paste that. We do the same thing with string02 and string03. I’ll just paste that there. I’ll do the same thing with string03. Of course, I’ll name them correctly. String 3, String 2. I’ll save the work. Let’s see what happens here. Once we save it, we go up and click Run, Run Script or type F5. Look what we have. It’s on a horizontal row, so it got rid of our new line characters so we can format our string values a little bit better as well. Again, it just removes the new line character so we can format each value a lot more cleaner as well. We can actually use operators, string operators as well.
Again, what the chomp operator does, all it does is it just removes the new line character at the end of a string value for better processing or we can use those string values in a much nicer task. If you guys have any questions, please feel free to harass me. I’ll definitely answer in the best way I can. Again, the chomp operator is just responsible for removing the backslash or the new line character to give you a string value without the new line character at the end or any extra leading, trailing characters at the end of the string value.