Perl Programming Page 5 - Perl Lists: More Functions and Operators |
You can use some operators on lists in a surprising way. While we don't have time to cover them all here, I figured I would spend this time showcasing some of them for you. For instance, there is the + operator. Let's see what happens when we add a list to itself: #!/usr/bin/perl @KoolAidFlavors = (@KoolAidFlavors, 'Grape ','Cherry ','Watermelon ','Fruit-Punch ','Orange '); print @KoolAidFlavors + @KoolAidFlavors; You may or may not (depending on how your brain works) expect this to result in: Grape Cherry Watermelon Fruit-Punch Orange Grape Cherry Watermelon Fruit-Punch Orange However, you would be wrong. Instead, the program counts the number of elements in the array and adds them together. Since there are five elements in our list, the result is: 10 Likewise, if we use the following code: #!/usr/bin/perl @KoolAidFlavors = (@KoolAidFlavors, 'Grape ','Cherry ','Watermelon ','Fruit-Punch ','Orange '); print @KoolAidFlavors + 2; We get the result: 7 Or the number of elements, plus 2. You will note that the subtraction operator works in the same way: #!/usr/bin/perl @KoolAidFlavors = (@KoolAidFlavors, 'Grape ','Cherry ','Watermelon ','Fruit-Punch ','Orange '); print @KoolAidFlavors - 2; Here the result will be 3, as 5 (the number of elements in the list) -2 is equal to 3. But what if we use the multiplication symbol (x, not *)? Here, we have a completely different result: #!/usr/bin/perl @KoolAidFlavors = (@KoolAidFlavors, 'Grape ','Cherry ','Watermelon ','Fruit-Punch ','Orange ') x 2; print @KoolAidFlavors; Now this will print the array twice. If we had put x3, it would print it three times and so forth. Note that I put the x2 at the end of the list. Had I put it after the print (and used * instead of x), then it would have simply multiplied he number of elements by 2 and returned the number 10. If I had put the x2 after the print command it would have returned 55, for reasons with which I am unfamiliar. The result of the above code is: Grape Cherry Watermelon Fruit-Punch Orange Grape Cherry Watermelon Fruit-Punch Orange Note that the value in @KoolAidFlavors is now all of the above. The code doesn't simply print out the value of the list twice; it stores it twice (or however many times you decide). Another operator we can use is the "..". This allows us to add sequential values to a list. It's best viewed in code: #!/usr/bin/perl @NumberList = (1 .. 20); print @NumberList; This will store the values from 1-20 in our list. When we print it out we get the result: 1234567891011121314151617181920 To further illustrate how this works, in the following code we assign the sequential values the same, but only print out the seventh element (remember that element number begin with 0): #!/usr/bin/perl @NumberList = (1 .. 20); print @NumberList[7]; The result of this code is: 8
This also works with letters: #!/usr/bin/perl @Alpha = (a .. h); print @Alpha; print "\n\n"; print @Alpha[3]; This will print out: abcdefgh d Note that this will not work in reverse; I couldn't type in 10 .. 1 and expect it to return: 10987654321 etc. Conclusion We covered a lot of ground in this article. In our next tutorial in this series we will go over the split() function, the List::Util, and maybe, just maybe touch upon those hashes I've been promising. There's only one way to find out though, and that's to drop by as often as possible. So do that. Till then...
blog comments powered by Disqus |
|
|
|
|
|
|
|