Perl
  Home arrow Perl arrow Page 4 - Beginning Programming Perl(s)
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

Beginning Programming Perl(s)
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2007-10-10


    Table of Contents:
  • Beginning Programming Perl(s)
  • Storing Data in A Variable
  • Quoth the Raven
  • List Data Types

  • 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


    Beginning Programming Perl(s) - List Data Types
    ( Page 4 of 4 )

    Sometimes you store stuff in a folder in real life. Now imagine that you have 26 folders in a drawer alphabetized from A-Z. Since you are a programmer, those files probably contain the names and addresses of girls that have rejected you. If you are a young programmer, you are only using one drawer. If you are older, well...you've switched from drawers to outdoor storage sheds. Either way, that is an example of a List Data Type.

    Simply put, a List is a group of scalars put in order. There are a variety of ways to order the scalars, which we will get into later on. If you are familiar with arrays in other programming languages, you'll know that a List is the same thing.

    Here is how you store data in a List:


    @my_cats = {'Joni ', 'Chachi ', 'Richie ', 'Fonzie ', 'Potsy '};

    The above creates a list with five scalars in it. If we do the following code:


    #!/usr/local/bin/perl

    @my_cats = {'Joni ', 'Chachi ', 'Richie ', 'Fonzie ', 'Potsy '};

    print @my_cats;

    This would print the following:


      Joni Chachi Richie Fonzie Potsy

    I can store numbers in Lists also, or mix numbers and characters if I choose. Perl doesn't care. It's too busy unwrapping that Hershey bar (the one with almonds).

    As you can see, Lists must have the @ symbol as the first part of their name. They use the lowercase naming convention (or at least I do) and it's best to separate words with underscores, though in truth you don't have to.

    So let's say a girl wants to use my cat program to see the names of all my cats. I'm clearly embarrassed to have so many (or I would be if I really did have that many). So I would create a program that would only show the name of one cat from my List.


    #!/usr/local/bin/perl

    @my_cats = {'Joni ', 'Chachi ', 'Richie ', 'Fonzie ', 'Potsy '};

    print $my_cats[1];

    This would print the name Chachi to the screen. I know what you are thinking and you can cool your biscuits right there (within arms reach...I'm hungry). Why didn't it print Joni? Simple. Lists start off at 0 and work their way up. To print Joni and Richie, you would use the following code:


    #!/usr/local/bin/perl

    @my_cats = {'Joni ', 'Chachi ', 'Richie ', 'Fonzie ', 'Potsy '};

    print $my_cats[0,2];

    You may have noticed in the above examples that I switched the @ symbol to the $ symbol on the last line. In addition to wanting to confuse you, this is also necessary because you are calling individual portions of the item (scalars) and not the entire item itself.

    You will note that the sample rules apply for quotes when working with arrays. If I wanted a list of contractions, I would do it this way


    #!/usr/local/bin/perl

    @my_contractions = {'Won't ', 'Can't ', 'Don't '};

    print @my_contractions;

    The above code prints:


      Won't Can't Don't

    Conclusion

    While we are not finished with data types or this series, I am finished with this current tutorial. In the next tutorial, we will wrap up Lists and move on to the third data type, and use operators to manipulate data.

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