Perl
  Home arrow Perl arrow Page 6 - Array Manipulation in Perl
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 4 stars4 stars4 stars4 stars4 stars / 139
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway