Perl
  Home arrow Perl arrow Page 6 - 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 - A Difficult Assignment
    ( Page 6 of 9 )

    You can assign the elements of an array to scalar variables, as in the following example:


    #!/usr/bin/perl 
    # define array 
    @human = ("John""Doe"); 
     
    # assign array contents to variables 
    ($fname$lname) = @human
     
    # print variables 
    print ("My name is $fname $lname");

    This won't work with an associative array, though - for that, you need the each() function. Every time each() runs on a hash, it creates an array containing two elements: the hash key and the corresponding hash value.


    #!/usr/bin/perl 
    # define hash 
    %matrix = ("hero" => "neo""villain" => "smith""teacher" => "morpheus""babe" => "trinity"); 
     
    # get first pair 
    ($character$name) = each (%matrix); 
    print 
    "$character = $namen"
     
    # get second pair 
    ($character$name) = each (%matrix); 
    print 
    "$character = $namen"
     
    # and so on... 

    The each() function comes in particularly handy when you need to iterate through an associative array, as it is well-suited for use in a "while" loop:


    #!/usr/bin/perl 
    # define hash 
    %matrix = ("hero" => "neo""villain" => "smith""teacher" => "morpheus""babe" => "trinity"); 
     
    # iterate through hash with each 
    # returns villain = smith hero = neo babe = trinity teacher = morpheus 
    while (($character$name) = each (%matrix)) 

            print 
    "$character = $namen"


    You can assign the array itself to another variable, thereby creating a copy of it, as below:


    #!/usr/bin/perl 
    # define array 
    @john = ("John""Doe"); 
     
    # copy array 
    @clone = @john
     
    # print copy 
    print ("I am a clone named @clone"); 



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