How do lists and arrays work?
- [Voiceover] A list is a fixed series of scalar values. For example, here is a working copy of hello.pl from chapter four of the exercise files, and if I come down here and say in parentheses, 1, 2, 3, and build and run, you see I get a 1, a 2, and a 3, all right next to each other like that. That's because in parentheses those three values, separated by commas, are a list.
A list is enclosed in parentheses and the elements in the list are separated with commas. You can easily iterate a list using foreach and we'll talk more about loops later in this course, but for now, foreach looks like this, or at least on kind of foreach looks like this. If I save and run, you see it says, 1, 2, 3 on separate lines so say is now being called foreach value in the list. List elements may be numbers, strings, or any scalar.
So instead of these numbers, I can use strings, and save and run, and you see we have that result that we expect. You may use the quote word operator to easily create a list of strings and that looks like this. And I'll save and run, and you see we get exactly the same result. Strings in a quote word list are bare. They do not use quote marks, and they're separated by white space.
It's very unique to Perl to have an operator like this, but it's actually very convenient, and you'll see it quite a lot. You may use the subscript operator to access individual elements of a list like this, foreach out of there and subscript it with a one. You'll notice that we'll get the second element, the number two. I'll save and run, so we get the number two. That's because subscripts are zero-based in Perl, so the first element is zero, the second element is one, et cetera.
On the other hand, an array is a variable data structure that holds a series of scalar values, so this would be an array. And I can iterate it with foreach like this, and save and run, and we get 1, 2, and 3 on separate lines just as we did with the list. So this statement here on line seven, it creates an array with the at symbol introducing the array variable.
And it initializes the array from a list, so in parentheses the 1, 2, and 3, separated by commas. That's a list of integer values that's being used to initialize the array named array with an at sign. Of course the elements in the array may also be strings or any scalar value, so you could just as easily do this using the quote word operator, and when we save and run, we get that result that we expect.
Now an individual element of an array may be specified with a subscript. So I can come down here and I can say $array, subscript one, and we'll get the number two there. So we see after we print that list, we get the number two at the bottom. It's important to note the dollar symbol here. That indicates that we're getting one scalar value from the array. If you use an at symbol by accident, you may notice that it works the same, or at least it looks like it works the same, at least in some context, but that's misleading.
It's actually returning the list with one value. So I can come down here and I can get the individual values like this, starting with 0, and then 1 and then 2, and build and run, and you see we have, let's scroll off there a little bit, make a little room there. So the most important distinction between a list and an array is that an array is mutable, that is I can change a value in an array. So if I want to change an element array, I can easily do it with a subscript.
So I could come down here and I could do this. I could say $array sub 1, and I could put it in quotes and say element one, and then come down here and repeat my foreach. And you'll notice that the second time element one there is in the second element which is, of course, indicated with the array subscript one. You may add and remove elements from an array.
You use push to add an element to the end of an array. So I can come here and I can say push @array, and now you notice that our array is much longer. We have one, two, three, and then I push four, five and six at the end of it. And you see so now we have one, two, three, four five, six, the second time we print it. You can use pop to remove an element from the end of an array. So if instead of this I say my $x = pop @array, and I'll come down here and I'll say $x after I repeat the array, and you'll notice that the second time through the array is smaller, one and two, and then I'm printing x with the say.
So if I comment out the say and we run, we see that the array only has two elements. And if I comment out the foreach, you'll notice that we're just showing the three there. So the pop removes the element from the end of the array and it returns that element so you can assign it to a variable if you like. And likewise, shift does the same thing but from the beginning of the array. So if I save and run, you'll notice that I have two, three, is what's left in the array, and one is what's in x.
And you can also use unshift to add an element to the beginning of an array or a number of elements. So I can put in a list, uno dos tres, and save and run. And we'll notice the first time through has one, two, three, and the second time through has uno, dos, tres at the beginning and then one, two, three. So using unshift, I've added that list of values to the beginning of the array.
You can get the number of elements in an array by using the scalar context of the array like this. And you notice that we have six elements in the array because we started with three and we added three with unshift. So lists and arrays are extremely powerful in Perl. In the next lesson, we'll continue by learning about array slices.
Watch to learn the details of the Perl syntax, from variables, conditionals, loops, and data structures to regular expressions, functions, and references. A quick-start guide is included for experienced developers who want to get up and running with Perl 5 fast, and the entire course is recommended for both new and experienced programmers alike. Later chapters cover file handling and reusing code with Perl modules, plus Perl best coding practices.
- Understanding Perl's general syntax and the anatomy of a Perl script
- Writing statements and expressions
- Creating assignments
- Working with variables and strings
- Using data types effectively
- Defining logical flow with conditionals and loops
- Using special variables
- Using Perl operators
- Performing simple Perl programming tasks with expressions
- Matching data
- Defining and calling functions
- Using references
- Handling files in the file I/O
- Using built-in functions
- Reusing code with modules
- Coding with Perl best practices