Perl
  Home arrow Perl arrow Page 2 - Perl: More on Data Types and Operators
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? 
PERL

Perl: More on Data Types and Operators
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2007-10-15


    Table of Contents:
  • Perl: More on Data Types and Operators
  • Hashes are Yummy
  • Operating the Deep Seas
  • Incremental/ Decremental
  • Comparing Numbers

  • 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: More on Data Types and Operators - Hashes are Yummy
    ( Page 2 of 5 )

    Writing, apparently, is hungry work. Or maybe I am just a greedy pig. For some reason, this next topic makes me think of food...hashes, hash browns...mmm, yummy. 

    Anyway, as I was saying, the third data type is Hashes. Hashes are like lists, but they aren't in order. Instead, they are indexed by a second set of scalars called keys. This might not make sense yet, but bear with me; it will.

    Observe the following masterful code written by yours truly:


    #!/usr/local/bin/perl

    %fat_people = {'James ', '400 ', 'YoMama ', '900 '};

    print %fat_people

    This will output:


      James 400 YoMama 900

    Please note several things. Because we used print to print the whole hash, it printed every value. Second, you will notice that the naming convention for hashes is the same as other data types, with the exception that hashes begin with a % symbol.

    So this is where Hashes begin to make sense. Let's say that I want to print only YoMama's weight from that hash. I would do so in the following manner:


    #!/usr/local/bin/perl

    %fat_people = {'James ', '400 ', 'YoMama ', '900 '};

    print $fat_people{'YoMama'};

    This would result in the print out:


      900

    In the above code, the weight 900 is printed because we used the index key, YoMama. Instead of lining everything up in order, Hashes use the first scalar as an index key, and the second as the data, and so forth. If I wanted to print the weight of James (or 400) I would have called James instead. Simple?

    Again, you will note that when I printed out the single result, I changed to the $ symbol, because again, I am printing only scalars.

    To make it a smidge more clear, you could also write the above hash this way:


    %fat_people = ('James ' => '400', 'YoMama' => '900'



     
     
    >>> 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 2 Hosted by Hostway
    Stay green...Green IT