Home
Username:
Password:
Perl Fundamentals Tutorials

Data Types / More about arrays

Subtitles of the Movie

So now let's take a look at some practical examples in Perl of putting together an array. Lets open up our text editor again and we'll see how we give an array some values. Now, when we initialize a scalar variable, all we did is we use the dollar sign and the name of the variable and then the is set to operator, followed by the value. An array works in much the same way. Except what we do, I'm going to going to initialize this array and I'm going to call it creatures. Let me use the is set to operator in just the same way, but then we use brackets, and when we put our values in we separate each of the values by commas. I'm going to put some arbitrary names of animals in there. And then when we're finished with our list, we close brackets and we put a semicolon at the end. And there's our array. Let's save that. And we're going to save that back in our testing directory, and I'm going to call this one array.pl. Now in order to access one of the elements of our array, all we need to do is type print creatures one in square brackets (i.e.- print $creatures). Don't forget the semicolon at the end. You'll notice also that as I was just mentioning earlier, although we were talking about the whole array we use the @ sign, when we're talking about one element within it we use the $ sign. Let's save that and we'll go over to our command prompt. And we're going to go to the relevant directory, and we're going to run our array Perl program. It returns - cat. If we look back at our script, you'll notice that the cat is the second element within the array. But because of our zero indexing that happens when we initialize an array, dog is element zero, cat is the element one, lion element 2, and elephant will be element 3. So we always have to remember to take away one when we're trying to work out which of the elements we're going to access. If we then wanted to add a new element to our array we can add one very simply by using this function push. Now the push function, we just type in the word push and then it takes two what we call arguments. We're going to look in more detail what functions do later. But for now all we need to know is that we use the function named push and then we put an open bracket. We can put a space in there or you can leave it out, depends on what you want. I usually put a space in just to make it a bit clearer. But there is no reason that you need to do that. So the 2 things that we feed to the function push are first of all, it wants the array name as its first argument. The things within the brackets here, within these brackets here and here are the arguments of the function. And the first argument is creatures, which is the array that we're pushing an element on to, that's the name. And the second argument is that the element that we're adding, in this case tiger. So if we save that and we're going to print creatures, element 4. Let's try again, and it prints out the cat followed by tiger. So it's very easy and quick to add a new element to an array after we've substantiated it. There's nothing set in stone about an array and after its initialized we can then do whatever we want to do with it. We'll take a look at some more things we can do with arrays in our next movie.

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