Home
Username:
Password:
Perl Fundamentals Tutorials

Data Types / Taking elements out of arrays

Subtitles of the Movie

Before we move on to look at hashes, let's take a look at some more things we can do to manipulate arrays and to work with them. Up till now, we've been creating our arrays like this: using the set to operator and then demarking every element, within the array with quotes and a comma. There's nothing wrong with this, but sometimes we will find it faster, and I am going to type out the same line again. In an exactly equivalent way to use the QW function. A QW function allows us to type in the same thing except without bothering to use any quotes or commas. So this line does exactly the same thing as we would have been able to do with the quotes and commas. Let's just show that exactly the same thing has happened here by trying to access the first element within the greetings array. Now if we run the array script there, it correctly finds the first element within the greetings array. Using the QW or Quote Workspace function is not always the best option. It's very good when we have a list of short array elements, which are all one word each. If we are going to use two words, then it has no way of telling where one elements stops and one element starts. So in that case I am probably better off using quotation marks, if there's going to be any ambiguity at all. So lets setup another array, this is going to be an array of weather. Each of this is a different weather condition. And now, how about if we want to remove an element from within the array? How about if we wanted to take away 'sun showers' out of our array, how do we do that? Well we've looked at pop, that's a function to take an element off the end. That's not very useful if we want to take off 'sun showers'. Maybe we could do it three times and that will take off three elements so we get rid of 'sun showers', but we'd also get rid of elements that we don't want to lose as well. The same goes if we are using shift. The function that allows us to use remove an element from wherever we want within an array, is the splice function. This takes three arguments, the first of which is the array that we want to manipulate, and the number of the element that we want to remove, and then the total number of elements that we want to remove. So we are going to start at one, which is the second element, and then we are going to remove only one element so we are only going to take away sun showers. Once again just like pop and like shift we can use this to output the value that we are actually taking out. When we say print, splice (@ weather, 1, 1); let's move over to here and test our script, we can see that it's taken sun showers and it actually removed that from the array. We can't see that here, but if we drop in new line and then we try to access the second element of the weather array, it's now changed to blue sky. So the splice command has been able to take away the second element there. The third argument here of splice, the number 1, if we change that to number 2 with our script again, it's actually taken out both sun showers and blue sky, and it's run those two together and that's what it's output as the output from the splice function. And then when we come to try to access the second element in the weather array, now it has actually moved on to drifting fog, because we've taken both sun showers and blue sky; we've taken out two elements from within 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