Perl Programming Page 9 - Array Manipulation in Perl |
You can alter the order of elements within an array with Perl's various array-sorting functions. The simplest of these is the reverse() function, which merely reverses the order in which elements are stored within an array:
The sort() function can be used to re-sort the elements in an array alphabetically:
The split() function splits a string into smaller components on the basis of a user-specified pattern, and then returns these elements as an array.
This function is particularly handy if you need to take a string containing a list of items (for example, a comma-delimited list) and separate each element of the list for further processing. Here's an example:
Obviously, you can also do the reverse - the join() function creates a single string from all the elements of an array, gluing them together with a user-defined separator. Reversing the example above, we have:
And that's about all I have for the moment. I hope you enjoyed this article, and that it offered you some insight into the types of things you can do with Perl's arrays. Should you require more information, try "man perlfunc" at your command prompt, or visit the "perlfunc" manual page on the Web. Until next time, stay healthy! Note: All examples in this article have been tested on Perl 5.8.0. Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article.
blog comments powered by Disqus |