Perl
  Home arrow Perl arrow Page 8 - Array Manipulation in Perl
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Array Manipulation in Perl
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 154
    2004-01-08


    Table of Contents:
  • Array Manipulation in Perl
  • Back to Basics
  • Hash Bang
  • Harnessing Elements
  • Looping the Loop
  • A Difficult Assignment
  • Push and Pull
  • Slice and Dice
  • Sorting Things Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Array Manipulation in Perl - Slice and Dice
    ( Page 8 of 9 )

    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:


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

    Or how about this one?


    #!/usr/bin/perl 
    # 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.


    #!/usr/bin/perl 
    # 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:


    #!/usr/bin/perl 
    # 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:


    #!/usr/bin/perl 
    # 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:


    #!/usr/bin/perl 
    # 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 Articles          >>> More By Harish Kamath, (c) Melonfire
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT