Perl Programming - Creating arrays
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 this lecture, we’re going to be talking about creating arrays. Again, this is section 4, lecture 2. Let’s jump right into it.
Creating arrays. First things first. What is an array? An array is just a list of different values in sequential order. Now, the cool thing about arrays is that they can contain multiple values of mixed type. So for example, you can have strings and values within your array versus a scalar variable can only hold one value at a time for a specific type: a number or a string, while with an array you can have either all numbers or all strings or a mix of both.
What is an array variable? An array variable is nothing more but a specific type of variable that contains the list of different values. Again, an array variable can hold many values rather than just one and again it could be a mixed type or mixed values.
Let’s take a look at creating an example of an array. The first thing first is that we have to use the @ symbol to declare our array followed by the array name. Again, we want to use the Perl identifier rules which you cannot use digits first to declare your array name. After that, we use our assignment operator which is just the equal = sign followed by parentheses () and we just include our values that we want to use followed by a comma after every value. As you can see, I’m using a mixed type which is the first one is strings which I use double quotes and then the second one is 44 which is just a number. Of course the third value is again a string and a fourth is 89 which is a number and the fifth is Jackson which is string. Again, we can use mixed type or a specific type which is just numbers or strings.
Let’s take a look at some examples for you to see live. The first thing we want to do is declare our array variable. We use again the @ symbol or the @ character followed by our variable name. I’ll just call it my _array as in our lecture. Again, I’m following the rules of our Perl identifier using letter characters and underscores and I want to assign some value. So I’ll put our assignment operator or our equal sign followed by parentheses on the right-hand side. I want to include some values. I’ll include a mixture-type of strings and numbers. I’ll type in my name in the string, put a comma for more values added. I’ll put 40, another comma to add more values. I’ll just put Harrison as a string. I’ll add another number.
I want to display these values to see if I set it up correctly. I’ll do a print statement. Inside the print statement, I’ll put double quotation marks followed by my very good character, the new line character. So I want to add a new line for nice formatting. I’ll take my array, the whole entire variable including the @ symbol. I’ll paste that in. Save my work. I’ll go up to the menu, click Run, Run Script. Let’s see what happens.
Again, as we can see, it printed out nicely with spaces included our list of values and our array. Again, we have Victor 40 Harrison and 30. Let’s see what happens if we just use the array variable with no double quotation marks. I’ll save my work, go back to Run. Click Run Script. It still prints out the same values but they’re all bunched together. So when you want to print an array of all the values with a space included automatically, just include the double quotation marks. It prints it out with space included. Again, I’ll show you this. Save my work. It looks a lot more cleaner and it included a new line character.
What I’ll do again is I’ll take this line of code and just make a copy of it and modify bits and pieces. I’ll just call this numeric_array and I’ll just include all numbers. I’ll do 1, 2, 3, 4, 5. Let me just make another copy of this and I’ll call this my string array. I’ll change numeric to string and I’ll give these all string values. Let’s say 1, 2, 3, 4, and lastly 5. I’ll save my work and I want to print all of these arrays. Let’s add a double new line character for even nicer formatting. I’ll save my work like usual. Copy our print statement. I’ll just make two copies of those for each array. I’ll take my numeric array first, paste it in there. I’ll take my string array and paste it in here. As we can see, we have our print statements all set up with our array. Let’s run it and test it. Look what we have. Our first array: Victor 40 Harrison 30. Our second array: 1 2 3 4 5. Our third array which contains all strings: one two three four five.
Pretty much in a nutshell that’s how we declare our array variable and create our values to our array. If you guys have any questions, please feel free to let me know. I’ll see you guys in our next lecture.