Data Types / More array manipulation
Subtitles of the Movie
Now there's another thing that we can do using the splice function. And let's take a look at that. We can take out two elements within the array there, and we can actually replace with another element. So say you ed to put in thunder, let's try that. Once again it took away sun showers, it took away blue sky, but it put it's own element back in, in it's place. So that's the fourth argument of the splice function, that is to interpolate an extra list in the same place as the place where we took out the elements. So this splice function is actually very multi-purpose, you can use it to do all sorts of things. For instance if we took the number two, made it zero, we are actually telling the splice function, if we look at the arguments in order, first of all this is the array that we want to work on. Then this is the place in the array we want to work on. And this is the number of elements we want to remove. And now we set it to zero so that this time the splice function is not actually going to remove any elements at all. But it is going to put extra one in its place, thunder. We don't just have to put one in, we can put in snowstorms, as well if we wanted. So now when we run our script, the splice command echoes nothing, because it has taken no elements away from our array. Once again we've put in thunder and we've also put in snowstorms into the array. And it's pop those in at the position one, which is second element, and sun showers, blue sky and drifting fog have all moved up to make room. Another thing we can do to our array would be to sort it. We can sort our array like this. We are asking Perl now to sort our array alphabetically. Now we got blue sky comes first, because it begins with 'B', drifting fog and so on. Now this doesn't work in quite the way, you might imagine it at first sight. If then after sorting our array, sorting our weather array, we then try and print it again, let's go back to our command prompt and see what happens. That's not too clear, I am going to put in there is, an extra new line break so that we can see the two print functions separately. Now the first of these here - blue sky, drifting fog is clearly arranged alphabetically. The second one is in the order in which we put it before the sort happened. So what's happened there? Lets take a look back. Our sort function is not actually changing the order of the array. What it's doing is just taking the array and producing a set of output based on sorting the weather array alphabetically. If we wanted to actually make the weather array sorted permanently, we'd have to do this. We have to take the weather array here, sort it, and then set the value of the weather array to the output from the sort function. although this may seem like a couple of extra keystrokes here, it does allow us to greater degree of flexibility. We are able to perform functions like sort on our arrays, but we don't necessarily have to alter what's in the array permanently. We can just do it for a second and then we can undo it. Another thing we can do is to reverse its contents. So now thunder comes first. Once again however the reverse function works exactly the same way as sort function. In that it doesn't make any changes to the contents of the weather array, unless we specifically tell it to by doing something like this.
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
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 