Home arrow Perl Programming arrow Page 9 - Array Manipulation in Perl

Sorting Things Out - Perl

perlOver the course of this tutorial, I'll be examining Perl's arrays in detail, explaining what they are, how they work, and how you can use them to get things done faster, better and cheaper. In addition to providing a gentle introduction to Perl arrays and hashes in general, this article will also offer you a broad overview of Perl's array manipulation functions, providing you with a handy reference that should help you write more efficient code.

  1. Array Manipulation in Perl
  2. Back to Basics
  3. Hash Bang
  4. Harnessing Elements
  5. Looping the Loop
  6. A Difficult Assignment
  7. Push and Pull
  8. Slice and Dice
  9. Sorting Things Out
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 171
January 08, 2004

print this article



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:

# define array 
@stooges = ("larry""curly""moe"); 
# reverse it 
@segoots reverse(@stooges); 
# reversed array now looks like this 
@segoots = ("moe""curly""larry"); 

The sort() function can be used to re-sort the elements in an array alphabetically:

# define array 
@stooges = ("larry""curly""moe"); 
# sort it 
@sorted sort(@stooges); 
# sorted array now looks like this 
@sorted = ("curly""larry""moe"); 

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.

$str "I'm not as think as you stoned I am"
# split into individual words on whitespace delimiter 
# and store in array @words 
@words split (/ /, $str); 

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:

$str "Rachel,Monica,Phoebe,Joey,Chandler,Ross"
# split into individual words and store in array 
@arr split (/,/, $str); 
# print each element of array 
foreach $item (@arr


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:

@arr = ("Rachel""Monica""Phoebe""Joey""Chandler""Ross"); 
# create string from array 
$str join (" and ", @arr); 
# returns "Rachel and Monica and Phoebe and Joey and Chandler 
# and Ross are friends" 
print "$str are friends"

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.

>>> More Perl Programming Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: