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

Slice and Dice - 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



Perl allows you to extract a subsection of an array - a so-called "array slice" - simply by specifying the index values needed in the slice. Consider the following example:

# define array 
@rainbow = ("red""green""blue""yellow""orange""violet""indigo");
# extract slice "blue", "yellow" 
@slice = @rainbow[2,3]; 

Or how about this one?

# define array 
@rainbow = ("red""green""blue""yellow""orange""violet""indigo"); 
# extract slice "blue", "violet", "red", "indigo" 
@slice = @rainbow[2,5,0,6]; 

You can also use a negative index for the "start" position, to force Perl to begin counting from the right instead of the left.

# define array 
@rainbow = ("red""green""blue""yellow""orange""violet""indigo"); 
# extract slice "indigo", "red" 
@slice = @rainbow[-1,-7]; 

Perl also comes with a range operator (..) which provides an alternative way of extracting array slices. Here's an example:

# define array 
@rainbow = ("red""green""blue""yellow""orange""violet""indigo"); 
# extract elements 2 to 5 
# slice contains "blue", "yellow", "orange", "violet" 
@slice = @rainbow[2..5]; 

You can also use the range operator to create arrays consisting of all the values in a range. For example, if you wanted an array consisting of the numbers between 1 and 20 (both inclusive), you could use the following code to generate it automatically:

# define array 
@= (1..20); 

The splice() function allows you to delete a specified segment of an array and splice in one or more values to replace it. Here's what it looks like:

splice(array, startlengthreplacement-values

where "array" is an array variable, "start" is the index to begin slicing at, "length" is the number of elements to remove from "start", and "replacement-values" are the values to splice in.

Here's an example:

# define array 
@rainbow = ("red""green""blue"); 
# remove elements 1 and 2 
# replace with new values 
splice (@rainbow12"yellow""orange"); 
# array now looks like this 
@rainbow = ("red""yellow""orange"); 

>>> 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: