Data Types / More array functions
Subtitles of the Movie
Other things that we can do with our array would be to take a creature away from the array. One thing that we can do to do that, is to use the shift function. What the shift function does is to remove the first element from the array, in this case dog. So if we use our shift function here, and on the next line we ask Perl to print the first element or element within the creatures array, let's see what we get, we get cat. If we look back at our script, we will see that when we first initialized the array, the first element was dog; and now that we've take one off the element has actually disappeared and all the others are shifted up to take its place. So cat has been moved into element number zero, lion into element number 1, and so on. The shift function, as well as removing an element from the array, also returns a value. So if we type print shift creatures, let's go back to our command prompt here, and as you can see it's actually printed first element of the array. So when we ran the shift command, not only did it remove dog, but it also brought it out for us to use it if we wanted it. What we are doing here effectively is feeding the output of the shift function, and giving that as input to the print function, which in return passes the output back to us, the user. Then when we run our second print command, we can see the other thing that shift did. Not only did it take the first element and give it to the print function, but it also removed it completely from the array. We can also use the unshift command, and we can do that/use that to add an element at the beginning of the array. So let's see what we get if we run our script again. This time we get snake as the first element or element zero. That's because when the unshift command will put snake in as element zero, and dog has to move up to make room, so does cat, so does lion, so does elephant and so does tiger. The counterpart to push is pop and that doesn't need any other arguments there, as we are taking away, we are not adding anything in. So all we need to do there is to use the pop function and we've taken the last element away again. So these two lines effectively cancel each other out. One adds the tiger and then pop takes it away again. One final thing that I am going to show that we can do with arrays before we move on, and take a look at more complex stuff is the join function. Now the join function allows us to pull together different elements within the array, and turn them into one scalar variable. So let's set a new scalar variable and we are going to call that 'zoo', and we are going to set that to the output of the join function. Functions typically take a couple of brackets after them. And that's where we put the arguments inside those brackets. So in this case we join it with this string here, a comma and a space and the word 'and' and then a space. And we mark that's one element on it's own by using two quotes marks around it. The second argument that this takes is the array itself, creatures. Once we've done that, let's printout the resulting scalar variable, let's see what we get . Perl is now being able to run all the elements within the array into one scalar variable. And it's been able to use whatever glue we fed it - in this case it's comma, space and space. But it could be anything we like - it could be, dot, dot, dot - in which case we get this kind of effect; or we can run them all together with nothing at all between them. In which case we just get all the words run into one long giant word. So now we can do some more things with arrays. And in our next movie we are going to take a look at a slightly complicated form of array called the multi-dimensional 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
United States 