Getting Started
Introduction
Perl IDEs
History
Advice
Tools
Mini-Tutorial
Tutorial
Code Snippets

Resources
Top Sites
More Tutorials
Books
Magazines
Articles
NewsLetters
Mailing Lists
NewsGroups
Forums
User Groups
Talk Shows
Blogs
Clothing

GBIC >> Perl >> Information Center Tutorials >> Hashes

Perl Information Center Tutorials - Hashes
These tutorials were written to help you get a quick, but thorough, understanding of Perl - the scope of the language as well as it's specific capabilities.

Beginners Built-In Functions     Advanced CGI Applications

Hash Functions Summary
Manipulation of hashes is one of Perl's strong points. The hash functions can be categorized roughly into four areas, depending on how they manipulate or return information about hashes.

    • Access Elements    
     keys, values, each
    • Delete Elements
     delete
    • Test
     exists

Hash Functions Reference
Here's a quick reference of the available hash functions, in alphabetical order.

  • delete - deletes a key/value pair
        delete EXPR
    
        delete $hash{$key}   
        
  • each - retrieves the next key/value pair
        each HASH
    
        while (($key,$value) = each %ENV) {
    	print "$key=$value\n";
        }  
        
  • exists - test whether a hash key is present
        exists EXPR
    
        print "Exists\n" 	if exists $hash{$key};
        
  • keys - retrieve list of keys from a hash
        @result = keys %ENV
        
  • values - retrieve list of values from a hash
        $result = values %ENV
        

Hash Code Snippets
The following code shows several methods of manipulating arrays.

     foreach $key (keys %myhash) { print "$key: $hash{$key}\n" }
     while (($key,$value) = each (%myhash)) { print "$key: $value\n" }

     %myhash = ()              # clear a hash
     delete ($myhash{$key})    # remove key/value pair
     if exists $myhash{$key}  # test for existence of a key

If you have suggestions or corrections, please let me know.