Perl Programming - Creating 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
How’s it going, everyone? Welcome back. In today’s lecture, we’re going to be creating lists. Again, this is section 4, lecture 4. Let’s jump right into it.
Creating a list. A list of an array indices can be assigned to a list of values. That’s all a list is. We can set up individual variables that’s in a list and have them assigned specifically to specific values instead of declaring an array and assigning all values. Again, we can just select specific values from an array and assign it to the values that’s in a list. The example is that we follow the parentheses on the left hand side. We actually set up our indices in the array and of course it’s followed by a dollar sign $ because again it’s a scalar variable and we want to assign a scalar value. If you look at this, it’s my array_name followed by the indice [] which is subscript to 0 which is the first value of the element. I set up the second one as well which is the second one. These are just my variables of my array. I just want to assign my first value and my second value which is King and Queen. Again, our first indice is going to be assigned King, and our second one is going to be declared with the value as Queen. That’s pretty much it in a nutshell when it comes to creating lists. We can just assign specific values to specific indices and an array and we can bypass creating a list of values to an array.
Also, you can use the qw operator to assign values to an entire array. The qw just stands for quoted words. Instead of using the parentheses and the quotation marks and the comma to assign values, we can just use it just like this. So qw followed by either parentheses or forward slashes. You can use different punctuations but I like to personally follow rules in programming. So I just use the parentheses to make it easier for myself and it reminds me of functions and methods. We can bypass using the double quotation marks on strings. They’re all separated by spaces, so I can put Jason Jackson Harrison. I can even include David followed by another space which is 44 and that will work as well just as if we’re using the normal declaration or creating a list of values for an array.
Let’s do some quick and fun examples. The first thing we do again let’s declare our array and assign it some values. We start off with the @ character followed by array name. I’ll just use my_array name to keep it basic, clean, and simple. I’ll set up some values, but this time, I’ll use the qw. Don’t worry about the forward slashes. You can use this as well versus the parentheses. For right now, I’ll just use my forward slashes the beginning and the end. I’ll just include spaces. Let’s just say Victor Jackson. As I promised, you can include numbers, so I’ll put 12 45. Right now my array holds four different values. Let me save my work because I get into the habit of saving my work. Let’s just print out this array to see if I’m correct. I’ll copy my array variable. Paste it in. Save my work. Click Run, Run Script. There we have it as well. Instead of using our parentheses, we use a shorter version operator called qw without using our quotes and commas. We can just start using this way for right now.
Let’s use our list now. Let’s create a list. Of course, we put our parentheses that creates and starts our list to assign indice values. I’ll put my assignment operator and our parentheses again. We use our array name, but to assign values, we use our scalar which is the $ followed by the array name followed by our indice number. I want to modify Victor with 0 which is our first value Victor. Let me just copy this. Let’s say I want to modify the number 012. The third element which has the indice of 2. So I’ll just modify Victor in the 12 and I’ll give it the values Davis and 56. Again, we’re just modifying the first element which is Victor to Davis and the third element which is indice 2 from 12 to 56. Let’s print these values out. I’ll do 0 and I’ll do indice subscript 2. Let’s see what happens. We Run, run our script. There we have it. We now modified and changed our scalar value to Davis. It went from Victor to Davis and from 12 to 56.
Again, this is really easy, guys. Simple. We’re just declaring some individual array values and we’re setting them up to the selected indices in our array. If you guys have any questions, let me know. I’ll see you guys in our next lecture.