Perl
  Home arrow Perl arrow Page 9 - 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 
IBM Developerworks
 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 - Sorting Things Out
    (Page 9 of 9 )

    You can alter the order of elements within an array with Perl's various array-sorting functions. The simplest of these is the reverse() function, which merely reverses the order in which elements are stored within an array:


    #!/usr/bin/perl 
    # define array 
    @stooges = ("larry""curly""moe"); 
     
    # reverse it 
    @segoots reverse(@stooges); 
     
    # reversed array now looks like this 
    @segoots = ("moe""curly""larry"); 

    The sort() function can be used to re-sort the elements in an array alphabetically:


    #!/usr/bin/perl 
    # define array 
    @stooges = ("larry""curly""moe"); 
     
    # sort it 
    @sorted sort(@stooges); 
     
    # sorted array now looks like this 
    @sorted = ("curly""larry""moe"); 

    The split() function splits a string into smaller components on the basis of a user-specified pattern, and then returns these elements as an array.


    #!/usr/bin/perl 
    $str "I'm not as think as you stoned I am"
     
    # split into individual words on whitespace delimiter 
    # and store in array @words 
    @words split (/ /, $str); 

    This function is particularly handy if you need to take a string containing a list of items (for example, a comma-delimited list) and separate each element of the list for further processing.

    Here's an example:


    #!/usr/bin/perl 
    $str "Rachel,Monica,Phoebe,Joey,Chandler,Ross"
     
    # split into individual words and store in array 
    @arr split (/,/, $str); 
     
    # print each element of array 
    foreach $item (@arr

            print(
    "$itemn"); 


    Obviously, you can also do the reverse - the join() function creates a single string from all the elements of an array, gluing them together with a user-defined separator. Reversing the example above, we have:


    #!/usr/bin/perl 
    @arr = ("Rachel""Monica""Phoebe""Joey""Chandler""Ross"); 
     
    # create string from array 
    $str join (" and ", @arr); 
     
    # returns "Rachel and Monica and Phoebe and Joey and Chandler 
    # and Ross are friends" 
    print "$str are friends"

    And that's about all I have for the moment. I hope you enjoyed this article, and that it offered you some insight into the types of things you can do with Perl's arrays. Should you require more information, try "man perlfunc" at your command prompt, or visit the "perlfunc" manual page on the Web. Until next time, stay healthy!

    Note: All examples in this article have been tested on Perl 5.8.0. Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

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