Perl
  Home arrow Perl arrow Page 4 - Perl Lists: A Final Look at List::Util
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

Perl Lists: A Final Look at List::Util
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-04-21

    Table of Contents:
  • Perl Lists: A Final Look at List::Util
  • Min(List)...Your Guide to Minimalism
  • MinStr(List) Like Them Small
  • Do the Shuffle

  • 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Perl Lists: A Final Look at List::Util - Do the Shuffle
    (Page 4 of 4 )

    Shuffle is a pretty simple function. It takes a list and returns it in a random order. That's it. Here it is in code:


    #!/usr/bin/perl

    use List::Util qw(shuffle);

    @nums = shuffle 0..10;

    print @nums;

    Here we create a list (@Nums) of random numbers by using shuffle. The numbers range from 1-10 and their order will vary each time you run this program. Here is my first result:

      014329685107

    Another way of sorting with shuffle is by using a predefined array:


    #!/usr/bin/perl

    use List::Util qw(shuffle);

    @Nums = (1,2,3,4,5,6,7,8);

    @Weird=shuffle(@Nums);

    print @Weird;

    Which once again gives us a randomly shuffled list. My first result was:

      25871643

    Sum()ming It Up

    Our final subroutine is sum(), which is used to sum up the numeric values in a list. Here are some samples:


    #!/usr/bin/perl

    use List::Util qw(sum);

    @Nums = (1,2,3,4,5,6,7,8);

    @Weird=sum(@Nums);

    print @Weird;

    Here, the result is:

      36

    Or 8+7+6+5+4+3+2+1

    We can also assign some wacky values using expressions in our list, and then sum up the total:


    #!/usr/bin/perl

    use List::Util qw(sum);

    @Nums = (1+1,2+2,3*3,4*8,5*7,6*6,7+4,8+2);

    @Weird=sum(@Nums);

    print @Weird;

    Here we are given the sum:

      139

    Lastly, we can find the sum of more than one list by using the following technique:


    #!/usr/bin/perl

    use List::Util qw(sum);

    @Nums = (1+1,2+2,3*3,4*8,5*7,6*6,7+4,8+2);

    @Nums2= (1,2,3,4,5,6,7,8);

    @Weird=sum(@Nums,@Nums2);

    print @Weird;

    This takes the two arrays, adds them together, and gives us the sum:

      175

    Conclusion

    And so we come to the end of our discussion of the List::Util subroutines. In our next tutorial, we will finally discuss the long anticipated Hashes, and perhaps, if not in the next article then the one after that, the Multidimensional list. There's a lot to cover, so be sure to drop by often.

    Till then...


    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.

       · Thanks for stopping by for another article in our series on Perl Lists and Hashes....
     

       

    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