Data Types / Working with arrays
Subtitles of the Movie
Now let's look at some practical examples of working with arrays. Let's take a look first at some examples that you might be familiar with from a non computer technical standpoint. One example is of an array, could be a dynasty of kings. In this case I've chosen king Richard. Now this array here in Perl, represents all the kings called Richard. You'll notice that we used an @ sign at the beginning of an array, where we use a dollar sign for a scalar variable. However, this @ sign is what we use when we're referring to the whole array - that is all of the kings called Richard. When we want to refer to one king within the array, then we're referring to one of the array elements and that's most likely a scalar. So what we do is we use the dollar sign again to show that we're referring to a scalar value, then we give the array name and then we use a pair of square brackets at the end and then the element number, in this case number 2. And it looks rather like king Richard the second. Now this is actually a little confusing at this point when you're not used to it, because this is not really king Richard the second, but king Richard the third. Now this may seem very counter intuitive. Why do we have a Richard followed by a zero here at the top? Surely it should start with one. Well, considering the fact that computers work on a binary system of zeroes and ones, zero is very important to the computer. It is actually the first number that it thinks of as the first number in a sequence. So, the first element here is zero, the second element here is one and the third element, or king Richard the third id actually marked slightly confusingly as Richard followed by the number 2 in square brackets. This may take a little bit of getting used to, but there are actually some other instances of this zero indexing we call it, outside of a technical framework. For instance, when we refer to the first century, the first century didn't start with the year 100. The first century actually started with the year 1. If they had known about zeros when they started the counting the years anno domini, they probably would have started that from year zero as well. But the difference is between the normal system of numbering, where we're expecting things to start with a one and then move on is that we have the first century here starting at one, then the second century actually only started at 100. By the time we get down to year 2000, we're calling that the 21st century. I remember that always confusing me as a child, but it has helped me to get to grips more quickly with the idea of zero indexing in arrays and I hope it does the same for you as well.
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 