Perl
  Home arrow Perl arrow Page 5 - Array Manipulation in Perl
FaxWave - Free Trial.
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 - Looping the Loop
    (Page 5 of 9 )

    Want to obtain the size of an array? Sure - just assign the array to a scalar variable and print the value of the scalar:


    #!/usr/bin/perl 
    # define array 
    @friends = ("Rachel""Monica""Phoebe""Chandler""Joey""Ross"); 
     
    # obtain size of array 
    $count = @friends
     
    # print size and contents 
    print ("I have $count friends: @friends "); 

    If you're using a hash, the keys() and values() functions come in handy to get a list of all the keys and values within the array.


    #!/usr/bin/perl 
    # define hash 
    %matrix = ("hero" => "neo""villain" => "smith""teacher" => "morpheus""babe" => "trinity"); 
     
    # returns an array of keys 
    @keys keys(%matrix); 
     
    # returns an array of values 
    @values values(%matrix); 

    Most of the time, size and key information is used in the context of a loop that iterates over the array. Here's an example of how this might work in a numerically-indexed array,


    #!/usr/bin/perl 
    # define array 
    @ducks = ("Huey""Dewey""Louie"); 
     
    # get array size 
    $size = @ducks
    print 
    "And heeeeeeeeeeeeeere are the ducks: "
     
    # iterate over array 
    for ($i=0$i<$size$i++) 

            print 
    "$ducks[$i] "


    and here's an example of iterating over an associative array using the keys() function:


    #!/usr/bin/perl 
    # define hash 
    %matrix = ("hero" => "neo""villain" => "smith""teacher" => "morpheus""babe" => "trinity"); 
     
    print 
    "Say hello to the characters in The Matrix: n"
     
    # get the keys of the array with keys() 
    # then use the keys to get the corresponding value 
    # in a loop 
    foreach $k (keys(%matrix)) 

            print 
    $k": "$matrix{$k}, "n"


    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 4 hosted by Hostway