Perl Programming - Performing Assignment With Lists
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, everyone! In this lecture, we’re going to be looking at performing assignments with list. Again, this is section 4, lecture 5. Let’s jump right into it.
List assignment. You can assign values to multiple scalar variables at once. The best thing about list assignment is that instead of creating multiple scalar variables and assigning values to them, we can do this just on one statement, just one line of code.
Our first example is that as you can see on the left-hand side, we are creating our list of scalar variables which is just x y z. Of course, on the right-hand side, we’re assigning multiple values to the list of variables. Of course, x gets 10. The second one which is y gets 20. Z gets 30. That’s one way to assign multiple values to a list of scalar variables.
Our second example is that we can actually use our handy-dandy quoted words operator. We can create a list of scalar variable names with the array indices on them as well, so 0 1 2. They will be in order with the quoted words. The first indice is Victor. The second one of course will be assigned Jaddian. The last one will be assigned Forte.
Let’s look at our third example. Our third example is we can actually reference the entire array. Of course we begin our array with an @ character followed by the name of the array which I just called names and I assigned the quoted word operator with a list of names which is Victor Jaddian and Forte. My array which is called names will have three elements with these values.
Let’s take a look at more examples and my live demonstrations. The first thing I’ll do is begin to set up my list. Our first one contained three scalar variables. Give me one second while I create my list. Let me just give them some spaces. I’ll call this name01. Let me just copy and paste these. I’ll take that. Paste that there. I’ll just rename them. Everything looks good. Of course I’m going to assign them values. I’ll call it Victor Jaddian Forte. That’s one way we can assign a list of values to our list of variables.
The second way is to of course we can create an array. Let’s just take these for example. I’ll just copy and paste those in here. But instead, we’re going to use the braces to reference our array element. That’s going to be 0. It’s going to be indice 1 and indice 2. Let me go ahead and do this again or another way. Let me use this for example, our list of values. I’ll just copy and paste those in and do that. What I’ll do as well is reference our last one. I’ll just call it names. This time, I’ll use our quoted words operator. This time I’ll use a pair of parentheses instead of our forward slashes just because I’m used to functions and methods with a pair of parentheses. But you can feel free to use the two forward slashes on the operator qw or quoted words. But for right now, I’m going to use a pair of parentheses. Of course, I’ll just give it the same values separated by spaces or one space. It should look like this Victor Jaddian Forte. Let’s just print out some of the list of values. I’ll take the first one. Of course we put our pair of double quotation marks to include our escape characters. This time I’ll go ahead and print the first one, second one, and the third one. I’ll just rename them name01, name02 and name03. I’ll just copy this line of code and just use them like this. I’ll reference these by my array and indices. I’ll take the last one and just reference the whole entire array. I want to print all the element values in that array. Let’s see if this works. If not, Perl will give us some beautiful, wonderful warnings and error messages that I can correct for you guys. Let’s run it. Run Script or F5. Congratulations! It worked just as I expected.
Again, these are just different ways to assign lists to a list of variables. You can use multiple values and assign them to multiple variables on one line or a single line of code instead of using multiple lines to assign values to variables as well. Again, we printed out all of the values in our variables. Pretty much that’s in a nutshell how to assign list of values to a list of scalar variables as well. If you guys have any questions, please feel free to let me know and I’ll do my best to get them answered. I’ll see you guys in our next lecture.