release 0.1
Sunday, 8 October 1995
A list of lists, or an array of an array if you would, is just a regular old array @LoL that you can get at with two subscripts, like $LoL[3][2]. Here's a declaration of the array:
Now you should be very careful that the outer bracket type is a round one, that is, parentheses. That's because you're assigning to an @list, so you need parens. If you wanted there not to be an @LoL, but rather just a reference to it, you could do something more like this:
Notice that the outer bracket type has changed, and so our access syntax has also changed. That's because unlike C, in perl you can't freely interchange arrays and references thereto. $ref_to_LoL is a reference to an array, whereas @LoL is an array proper. Likewise, $LoL[2] is not an array, but an array ref. So how come you can write these:
instead of having to write these:
Well, that's because the rule is that on adjacent brackets only (whether square or curly), you are free to omit the pointer dereferencing array. But you need not do so for the very first one if it's a scalar containing a reference, which means that $ref_to_LoL always needs it.
First, let's look at reading it in from a file. This is something like adding a row at a time. We'll assume that there's a flat file in which each line is a row and each word an element. If you're trying to develop an @LoL list containing all these, here's the right way to do that:
You might also have loaded that from a function:
Or you might have had a temporary variable sitting around with the list in it.
It's very important that you make sure to use the [] list reference constructor. That's because this will be very wrong:
You see, assigning a named list like that to a scalar just counts the number of elements in @tmp, which probably isn't what you want.
If you are running under use strict
, you'll have to add some
declarations to make it happy:
Of course, you don't need the temporary array to have a name at all:
You also don't have to use push(). You could just make a direct assignment if you knew where you wanted to put it:
or even just
You should in general be leary of using potential list functions in a scalar context without explicitly stating such. This would be clearer to the casual reader:
If you wanted to have a $ref_to_LoL variable as a reference to an array, you'd have to do something like this:
Actually, if you were using strict, you'd not only have to declare $ref_to_LoL as you had to declare @LoL, but you'd also having to initialize it to a reference to an empty list. (This was a bug in 5.001m that's been fixed for the 5.002 release.)
Ok, now you can add new rows. What about adding new columns? If you're just dealing with matrices, it's often easiest to use simple assignment:
It doesn't matter whether those elements are already
there or not: it'll gladly create them for you, setting
intervening elements to undef
as need be.
If you just wanted to append to a row, you'd have to do something a bit funnier looking:
Notice that I couldn't just say:
In fact, that wouldn't even compile. How come? Because the argument to push() must be a real array, not just a reference to such.
If you want to print the whole thing, though, you can't just say
because you'll just get references listed, and perl will never automatically dereference things for you. Instead, you have to roll yourself a loop or two. This prints the whole structure, using the shell-style for() construct to loop across the outer set of subscripts.
If you wanted to keep track of subscripts, you might do this:
or maybe even this. Notice the inner loop.
As you can see, it's getting a bit complicated. That's why sometimes is easier to take a temporary on your way through:
Hm... that's still a bit ugly. How about this:
Here's how to do one operation using a loop. We'll assume an @LoL variable as before.
That same loop could be replaced with a slice operation:
but as you might well imagine, this is pretty rough on the reader.
Ah, but what if you wanted a two-dimensional slice, such as having $x run from 4..8 and $y run from 7 to 12? Hm... here's the simple way:
We can reduce some of the looping through slices
If you were into Schwartzian Transforms, you would probably have selected map for that
Although if your manager accused of seeking job security (or rapid insecurity) through inscrutable code, it would be hard to argue. :-) If I were you, I'd put that in a function:
This function was designed to pop off the last element from each of its arguments and return those in a list. In this function, you can think of @_ as a list of lists.
Just as a side note, what happens if the function is called with the "wrong" types of arguments? Normally nothing, but in the case of references, we can be a bit pickier. This isn't detectable at compile-time (yet--Larry does have a prototype prototype in the works for 5.002), but you could check it at run time using the ref() function.
However, that's not usually necessary unless you want to trap it. It's
also dubious in that it would fail on a real array references blessed into
its own class (an object). But since you're all going to be using
strict refs
, it would raise an exception anyway even without the die.
This will matter more to you later on when you start building up more complex data structures that all aren't woven of the same cloth, so to speak.