Perl Programming - Accessing array indices
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
Hello everyone. Welcome back. In this lecture, we’re going to be talking about access array indices. Let’s jump right into it.
Accessing elements of an array. The array elements are numbered using integer numbers starting at 0. Again, to select a specific type of element in an array or an indice, we have to use a decimal number or a whole number to access each specific array element. For example, if we were to create an array, again we use our @ symbol followed by the array name. We declare using the Perl identifier. If it has two values, let’s say for example if we only want to select one value from that array, not the entire array, but each individual value of an array, so in Perl it gives us a way to do that. We can use a print statement of course to print all values in the array, but in order to do this and select only one part of it, it’s causing indices. So we have to use a dollar sign $ which indicates a scalar value followed by the array name. We have to include our square braces or our square brackets followed by the integer number. Again, guys, it starts with 0.
For example, the first value is Jackson. 0 is referenced to Jackson. Jackson is actually the first value of the array which is array[0] or subscript 0. Of course, we count up 1. So Williams is the second value in the array which is 1, so 0 1. 1 will reference Williams. That’s pretty much how you reference independent or individual element values. That’s how we retrieve a specific value from an array. We just use an indice which is a whole number to access that value of our array.
Let’s do some examples I can show you guys. The first thing we want to start off with is declaring and setting up our array. Let’s define an array and set up some values. Again we start off with our @ symbol followed by our array name. I’ll call it my_array. I’ll give it some values. Let’s say Test 1. Let’s just call it Test 1 for my value. I’ll just give it all strings this time to make it easy. So Test 1, Test 2, and Test 3. I’ll actually add a period. Let me save my work. What I’ll do as well, I’ll declare scalar variables. What I’ll do here let’s call it array_variable01. Remember to retrieve a value from an array or an indice of an array, we have to use the $ sign to reference a scalar value. We type the name of an array followed by our square brackets [] and the indice number. Again, it starts off as 0.
Let’s say I want to reference the first element which is 0. I want to reference the last element which is Test 3 which will be indice 2. Sorry about that. Like I said, 0 1 2. 2 is the last element inside our array. Again, I’ll call this array_variable_02 with our indice of 2. An easy way to do this is just count. Like I said it starts from 9 and you can go in the following order. Let’s print our values. We set up a print statement. I’ll take my first array variable. Of course I’ll put it in double quotation marks because I love the power of double quotation marks. I’ll add my nice new line character. Again, I’ll do the same thing for the second variable and add another new line character. If I’m correct, it’s going to print out Test 1 followed by a period and it’s going to print out Test 3. We save our work. Go up to the menu. Click Run, Run Script. There we have it: Test 1 and Test 3.
If we even assign another one to test out all three of them, let’s just put this variable in. I’ll do that. Paste it in followed by a new line. I’ll print out all three of them. Always remember to double-check the variable name. I’ll run it again and there we have it. It should have printed Test 1, Test 2, and Test 3. Always remember as well. Look for your errors. I’ll put this as indice 1. Actually it’s good to have errors because you can learn off your mistakes as well like me for example as well. Again, this is indice 1, indice 2, indice 3. It references all these values. Again, we save our work. Let’s see if I set up everything correctly. There we have it: Test 1, Test 2, and Test 3.
That’s pretty much how we reference indices of elements specifically to retrieve their value and to save them and to scalar variables. If you guys have any questions, let me know. I’ll you in our next lecture.