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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
SunQuest
 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: 5 stars5 stars5 stars5 stars5 stars / 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:
      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
     
    IBM developerWorks
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    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


       · Hey guys, thanks for dropping by and reading my article; I hope its helpful. Perl is...
     

       

    PERL ARTICLES

    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - 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

    Click Here




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway