Perl
  Home arrow Perl arrow Page 2 - 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? 
Google.com  
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 - Back to Basics
    ( Page 2 of 9 )

    I'll begin right at the top, with the answer to a basic question: what's an array, anyhow?

    In all programming languages, an array is a data structure that lets you store multiple values in a single variable. This structure is a very useful method of storing and representing related information. In Perl, array variables look like any other variable, with the exception that array variable names are always preceded by an @ symbol. Here's an example:


    #!/usr/bin/perl 
    # define array 
    @friends = ("Rachel""Monica""Phoebe""Chandler""Joey""Ross"); 

    Thus the array variable @friends contains six elements, the names of the "Friends" crew.

    The various elements of the array are accessed via an index number, with the first element starting at zero (this is sometimes referred to as "zero-based indexing"). So, to extract the first element of the array above, you'd use the notation $friends[0], while the notation $friends[5] would give you the sixth element of the array, "Ross". Here's a simple code snippet explaining this:


    #!/usr/bin/perl 
    # define array 
    @friends = ("Rachel""Monica""Phoebe""Chandler""Joey""Ross"); 
     
    # prints "Phoebe" 
    print $friends[2]; 

    In case you don't want to define the array all at once, you can define it incrementally, by assigning values to the array one at a time using the index notation above. For example, this line of code


    #!/usr/bin/perl 
    # define array 
    @friends = ("Rachel""Monica""Phoebe""Chandler""Joey""Ross"); 

    is equivalent to these:


    #!/usr/bin/perl 
    # define array 
    $friends[0] = "Rachel"
    $friends[1] = "Monica"
    $friends[2] = "Phoebe"
    $friends[3] = "Chandler"
    $friends[4] = "Joey"
    $friends[5] = "Ross"

    Notice that you don't have to do anything special to bring an array variable into existence, like declare it with a keyword or instantiate an object to hold its values - Perl identifies the notation being used and creates the data structure appropriately.



     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek