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

Perl Lists: A Final Look at List::Util
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    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:
      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


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



     
     
    >>> More Perl Articles          >>> More By James Payne
     

       

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek