Home
Username:
Password:
Perl Fundamentals Tutorials

Data Types / Array functions

Subtitles of the Movie

In our last movie we looked at some very basic stuff with arrays, how to initialize an array and how to add an extra variable and how to print out elements within that array. What if we wanted to find out the number of elements within an array? Now it may be very obvious in this particular very simple script, how many elements we are dealing with. It doesn't take a leap of mathematic to add 4 to 1. However, as our scripts get more complicated, we may find that it is difficult to keep track, or even impossible depending on the way in which our script is structured. So we may need to use our piece of Perl script to examine our array and see how many element it contains. What we can do is we can set a scalar variable and we can pull this num creatures or number of creatures, and we're going to simply set that to the number of elements within the creatures array. We can do this very simply like this. We're setting the scalar variable and we appear to be setting it to the same as what the array is set to. However, we can't set the scalar variable to be an array. That would break the meaning of scalar versus the array data types. So what perl does is it tries to convert something meaningful about the array into a scalar context. In this case, it's going to take the size of the array or the number of elements. So if we now try and print value of num creatures, go into our command prompt to test that, we can see that it's able to pick up the number of creatures in the array by using that particular line of Perl script. Another thing we might want to do is to use this variable we've created to show say the second to last creature in the array. So to do that, we can use the creatures array, we put a dollar sign before that to show we're referring to one element within it only. Then we can use square brackets around here and we're going say num creatures minus two. Let's take a look at our command prompt and see what that comes up with. It's the elephant. Now let's take a look back. Now, it may seem, since we've taken the total number of creatures and used that as the element that we're trying to access within the creatures array, that it should go back to the lion. The tiger's the last one, the elephant is the second last one, but if we take two away we get a lion. The reason why this is not happening is that the element that would exist if we were able to access the element numCreatures within the creatures array, hasn't actually been created yet. That's because our array is zero indexed. So, imagine if we had an array with just one element, dog. The length of the array would be one. But the element that we're able to access would be called zero. If we try to access creatures , we wouldn't get a result. Let's see what happens if we try to access creatures and the element we're trying to get is the same number as the size of the array. If we go back to our script, we get nothing. That array element has not been set. So we don't get any output back. We're looking for an element that doesn't exist. So that's an important thing to remember, as well as it works at both ends of the array: as well as the first element being zero, the last element is always one less than the length of the array.

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