Perl
  Home arrow Perl arrow Page 4 - Perl: Another Round with Hashes
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: Another Round with Hashes
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-05-05


    Table of Contents:
  • Perl: Another Round with Hashes
  • Adding More Than One Record
  • Using the Delete Function
  • Storing a Deleted Value in a Variable

  • 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: Another Round with Hashes - Storing a Deleted Value in a Variable
    ( Page 4 of 4 )

    We don't have to simply do away with our deleted value. In the following code, we will store it in a variable and append it to a sentence:


    #!/usr/bin/perl

    %Wrestlers=(Champ=> ' CM Punk ', Chump => ' Chavo Guerrero ',

    OldSkool=> ' Big John Stud ', Boof=> ' Brutus Beefcake ');

    print values(%Wrestlers);

    print "\n\n";

    $Dead=delete(@Wrestlers{Champ});

    print values(%Wrestlers);

    print "\n\n";

    print "This guy is dead: " . $Dead;

    Here we have:

      Brutus Beefcake Big John Stud CM Punk Chavo Guerrero

      Brutus Beefcake Big John Stud Chavo Guerrero

      This guy is dead: CM Punk

    And finally, we come back to our scenario where both Chavo and CM Punk die. Now we would like to add them to a growing list of dying wrestlers. Since seeing is believing, here is the eye-catching code:


    #!/usr/bin/perl

    %Wrestlers=(Champ=> ' CM Punk ', Chump => ' Chavo Guerrero ',

    OldSkool=> ' Big John Stud ', Boof=> ' Brutus Beefcake ');

    print values(%Wrestlers);

    print "\n\n";

    @Dead=delete(@Wrestlers{Champ,Chump});

    print values(%Wrestlers);

    print "\n\n";

    print "These guys are dead: " . @Dead[0] . @Dead[1];

    This will delete the records for 'Champ' and 'Chump' and store them in the list @Dead. When we run this program we get the following printout:

      Brutus Beefcake Big John Stud CM Punk Chavo Guerrero

     

      Brutus Beefcake Big John Stud

      These guys are dead: CM Punk Chavo Guerrero

    One final note. Had you tried to write out both values in the @Dead list like so:


    print "These guys are dead: " . @Dead;

    You would have been given the sentence:

      These guys are dead: 2

    Instead of printing the values, it would have printed the number of elements. Why that is, I am not certain.

    Well that's all the time we have for this one. Be sure to stop by next time as we continue talking about hashes and go into those multidimensional lists you've been hearing so much about.

    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