Perl Programming - Working With Scalar Data
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, guys, to Section 3. In this section, we’re going to be looking at working with scalar data. So let’s move right along.
What is scalar data? A scalar is the simplest kind of data that Perl can modify. Scalars can only hold one value at a time. Scalar values are usually numbers or strings of characters in Perl. Scalar values can be used with different operators such as the addition operator, minus, subtraction, multiplication, division and other mathematical operators that we’ll see as well in later lectures, and it will return back a result to us with the calculated result. The scalar results can be saved into a scalar variable as well. They can also be read or written from files or the devices.
So let’s jump into some examples now. Welcome to our examples. I’m going to be giving demonstrations of Scalar values. The first thing is I want to set up some containers or variables to store our Scalar values. I’ll start with a container or variable called Variable_01 and I will assign the Scalar value as a string of characters. So I’ll say, “Hello Class, My name is Victor Davis.” I’ll give it two new lines to return. I’ll just set up another variable and I’ll call this Variable_02. Again, I’m just giving these Scalar values some meaning. So for the second string with just single quotes, I’ll type ‘Today is a wonderful day.’ I’ll create another one. This time, I’ll use some numbers. I’ll type 50. Variable_04, I’ll give it 34.65. Just give it a random number. So all of these values on the right-hand side of the equal sign are considered single Scalar values that we can use in a program or Perl application.
Let’s print out our values. If I take Variable_01 and of course let me add a new character, it already has a new line character. Let’s save our work. We can run it. Run Script. Of course, it printed out “Hello Class, My name is Victor Davis.” We can check all of these Scalar values. So I’ll change the 01 into a 02, save our work. Click F5. Today is a wonderful day. Press any key to continue. Again, I’m just checking our values and our variables. Variable_03 is 50. Then of course Variable_04 is 35.65. So as we know, we can save only one value inside our container because Scalar values are really considered only one value.
Let’s do some math on our Scalar data. If I do Variable_03 * Variable_04, what it’s really saying is 50 multiplied by 34.65, what is the value of that? So I’ll save that and I’ll click F5. So there we have it. 1732.5 is the answer to 50 * 34.65.
That’s pretty much it for our Scalar values. Again, they’re just numbers or strings that Perl recognize as single values. Again if you guys have any questions, feel free to bug me and I’ll see you in our next lecture.