Data Types / Multidimensional arrays
Subtitles of the Movie
A multidimensional array is not quite as otherworldly as it might sound at first hearing. It's quite a simple concept really. If we look at a practical example of the way in which arrays can be arranged within different dimensions, we can see in what sense it can make within the context of programming Perl. One-dimensional array could consist of three locations, left, middle, and right. However because we are dealing in one dimension, we can't have any concept forward or backward, up or down and so on. Two-dimensional array however could take all the points of the compass and map them out onto a board here, rather like a noughts and crosses board or like the points of the compass. So let's take a look at the way that would work, within the context of a Perl script. If we were going to map the points of a compass onto an array, how do we do it? We could string them out one after the other: north, northeast, northwest, south, southeast, southwest. But that wouldn't really be maintaining the structure of the points of the compass. What we would be doing instead would be putting them all into a long line, and then it would be difficult to easily access the right one, if we wanted to refer to it later on down the line. Instead of doing that, what we could do is to create a multidimensional array. And multidimensional could just be two dimensions. Probably won't need more than two dimensions, at least not to begin with anyway. So let's take a look at a way that we can do that. We are going to create an array called compass and we are going to need to put in 9 values. The 8 points of the compass and a central value as well, just for the sake of completeness. Instead of just putting brackets, followed by north-east so on, what we are going to do is we are going to use square brackets as well inside, to demarcate each line of the multi-dimensional array or the two-dimensional array in this case. So we are going to include northeast, north I should use west first actually, follow the way that it appears on paper. Close these square brackets then we use another line, comma to show another line of an array, and then it's west, center, east, and then close square brackets, another comma and then southwest, south and southeast. Close square brackets and close the round brackets and we've created our multi-dimensional array. This is not very easy to read, is it? Whatwe can do to make this kind of thing easy to read is to use indentation. We can split this kind of complicated lineup over 5 different lines. Although it takes up all more space, it also makes our program a lot easier to read. We can now still see the shape of the compass within our Perl script. That's the great thing about using the semicolons at the end of the lines of code. That means we don't have to worry about each line of our program, being as treated something separate, because until Perl gets to a semicolon, it's going to treat everything that we type as one line. So that's why the semicolons are a vital part of your Perl scripts. So what this line of code here is doing is, it's effectively creating an array, then it's creating three smaller arrays within it. That's all a multi-dimensional array is. It's as simple as that: an array of arrays. When we want to actually access an element within that array, we can simply type print, once again we are going to use scalar context, we are aiming at one element within the array, and we are going to type zero which means that we are going to be looking at the top line there. Then we also have to refer to an element within that inner array. So how do we do that? We put a little minus sign followed by greater than to create what looks like an arrow. This is actually a reference, but we are not going to look too deeply into that at the moment, just suffice it to say that this is the reference operator here, and we are able to access the second element of the first inner array of the compass two-dimensional array. Let's see what happens when we run that. We get N; let's take a look back at our Perl script, this here represents the first line, because we start with zero once again. And how do we tell which column we are going for? We've got one there, so if we count them, this is element zero and this will be element one, or the second element. So that's how we create and access elements within a multi-dimensional array. This will become very useful as you need to create more complex data structures than just a simple list. Even if you don't use it right away, it's something that's good to bear in mind, and it's good to recognize when you are looking at other people scripts, so that you can work out what they are driving at.
Tutorial Information
| Course: | Perl Fundamentals |
| Author: | Joshua Mostafa |
| SKU: | 33403 |
| ISBN: | 1-9320-7215-2 |
| Release Date: | 2002-12-19 |
| Duration: | 7.5 hrs / 113 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 