[INSTRUMENTAL MUSIC]
Today, we're talking about working with arrays in Perl:
ordered collections of scalar values.
To create an array in Perl, we must
assign a variable with the '@' sigil from a list of values.
In Perl, a list is a parenthesised,
comma-separated, collection of values.
To set or retrieve one of those values,
I use square brackets to index or subscript that array.
In Perl, arrays are zero-indexed.
If I'm requesting one variable, I use the '$' sigil.
And I can assign it new values.
There are other ways to produce a list of values
other than just specifying them literally.
One is the 'quote word' operator,
which takes a delimited list of values
and splits them by white space.
Another way to produce a list of values
is to use the range operator--
two dots-- to produce a sequential range of values.
Integers are common, but strings work here too.
Now if I want to retrieve or modify
multiple values in the array,
I can index with a range of values.
And I use the '@' sigil in an operation known as "slicing".
If I assign values from a shorter list here,
empty values are filled with 'undef'.
If I assign values from a longer list, that list is truncated.
Unlike C, arrays in Perl have a known length and can vary
in size as our program runs.
To find the current size of an array,
we cast it into the scalar context
with the 'scalar' keyword.
We can also use '$#a' to find the greatest
existing index.
I can grow and shrink an array in many ways.
One common way is to treat it as a stack or a queue.
Perl defines 'push' and 'pop' on arrays.
'push' lets as easily append one or more values,
returning the new number of values in the array.
'pop' consumes the last value in the array.
Similarly, I can prepend one or more values
to the array using 'unshift'.
And I can remove the first value with 'shift'.
These are useful for dealing with, for example,
command line arguments or subroutine parameters,
which in Perl, are both provided as arrays.
Let's say I have some data.
Urgh. I've written a subroutine, 'showA',
which shows me that list of values a little bit more
compactly.
So here's that data again.
Now let's say I want to reverse this.
I could faff around with a loop.
Or I could just use 'reverse'.
What if I wanted to sort my data?
Sure, again, I could implement my own sort.
But I really want to just get things done.
So I just use 'sort'.
Surprise!
'sort' produces a naturally sorted list of values.
It uses lexicographic comparison.
So it only works sensibly on strings, not on numbers.
So what if we want a numeric sort,
or some other particular order?
Well, we can provide our own comparison block,
which defines an expression that's evaluated for
pairs of values, '$a' and '$b',
to determine the order they should be in.
It can be arbitrarily complex too.
What if I wanted to find a value?
Well, I can use 'grep', which takes a block
defining a filter and a list of values.
And it returns only those values
where that filter evaluated to true.
What if I wanted to transform all the values?
Well, I can use 'map', which, again,
takes a block defining a transformation,
and a list of values.
And it returns the list of values
with the transform applied to each.
Chaining 'sort', 'grep', and 'map' together
can yield powerful pipelines of
value filtering and transformation.
We've only just scratched the surface of what
we can do with arrays in Perl.
For a longer list of array techniques,
check out the Perl documentation collection.
[INSTRUMENTAL MUSIC]