1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Introduction To Hashes In Perl

Discussion in 'Perl' started by pradeep, Jan 28, 2008.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    A hash is a data structure that associates keys with values. The primary operation it supports efficiently is a lookup: given a key (e.g. an employee's id), find the corresponding value (e.g. that employee's name). It works by transforming the key using a hash function into a hash, a number that is used as an index in an array to locate the desired location ("bucket") where the values should be.

    To define a hash in Perl, we use the percent (%) symbol before the name.

    Declaring/Initializing/Clearing/Empty A Hash



    Code:
       #!/usr/bin/perl
       
       use strict;
       
       my %hash;        # declaring a hash
       my %hash = ()        # initializing an empty hash / Clear,Empty an existing hash
       
       ## HASH REFERENCE
       my $hash_ref = \{};    # reference to an empty hash
       

    Adding A Key/Value Pair To A Hash



    Code:
       #!/usr/bin/perl
       
       use strict;
       
       my %hash;        # declaring a hash
       my $hash_ref = \%hash;
       
       $hash{'myKey'} = 'myValue'; # quotes around the keys can be omitted when the keys are identifiers.
       $hash{$myKey}  = $myValue; # hash, using variables
       
       ## ADDING TO A HASH REFERENCE
       $hash_ref->{'myKey'} = 'myValue';
       $hash_ref->{$myKey}  = $myValue;
       

    Deleting A Single Key/Value Pair



    Code:
       #!/usr/bin/perl
       
       use strict;
       
       my %hash;        # declaring a hash
       my $hash_ref = \%hash;
       
       delete $hash{$key};
       
       ## DELETE HASH REFERENCE
       delete $hash_ref->{$key};
       

    Iterating Over The Hash Key/Value Pair



    There are numerous methods to iterate a hash, here are some of the following.

    Method 1

    Using each within a while loop. Note that each iterates over entries in an apparently random order, but that order is guaranteed to be the same for the functions keys and values.

    Code:
       #!/usr/bin/perl
       
       use strict;
       
       my %hash;        # declaring a hash
       
       ## Add some dummy data to the hash here
       
       while(my ($key, $value) = each(%hash)) 
       {
           print "$key => $value\n";
       }
       
       my $hash_ref = \%hash;
       
       ## HASH REFERENCE
       while(my ($key, $value) = each(%$hash_ref)) 
       {
           print "$key => $value\n";
       }
       
    Method 2

    Using keys with a for / foreach loop.

    Code:
       #!/usr/bin/perl
       
       use strict;
       
       my %hash;        # declaring a hash
       
       ## Add some dummy data to the hash here
       
       for my $key ( keys %hash ) 
       {
           my $value = $hash{$key};
           print "$key => $value\n";
       }
       
       my $hash_ref = \%hash;
       
       ## HASH REFERENCE
       for my $key ( keys %$hash_ref ) 
       {
           my $value = $hash_ref->{$key};
           print "$key => $value\n";
       }
       
    Method 3

    Using map.

    Code:
       #!/usr/bin/perl
       
       use strict;
       
       my %hash;        # declaring a hash
       
       ## Add some dummy data to the hash here
       map {print "$_ => $hash{$_}\n";} keys %hash;
       
       my $hash_ref = \%hash;
       
       ## HASH REFERENCE
       map {print "$_ => $hash->{$_}\n";} keys %$hash;
       

    Size Of A Hash



    Method 1

    Code:
       #!/usr/bin/perl
       
       use strict;
       
       my %hash;        # declaring a hash
       
       ## Add some dummy data to the hash here
       print "size of the hash:  " . keys( %hash ) . ".\n";
       
    Method 2

    Code:
       #!/usr/bin/perl
       
       use strict;
       
       my %hash;        # declaring a hash
       ## Add some dummy data to the hash here
       
       my $hash_ref = \%hash;
       my $i = 0;
       
       $i += scalar keys %$hash_ref;  # method 1: explicit scalar context
       $i += keys %$hash_ref;         # method 2: implicit scalar context
       

    Sorting Hashes by Key



    Code:
       #!/usr/bin/perl
       
       use strict;
       
       my %hash = (23,'pradeep',26,'shabbir',34,'asha',15,'tanaz'); # declaring a hash
       
       # sort using foreach
       foreach my $key (sort keys %hash)
       {
           print "$key => $value \n";
       }
       

    Sorting Hashes by Values



    Code:
       #!/usr/bin/perl
       
       use strict;
       
       my %hash = (23,'pradeep',26,'shabbir',34,'asha',15,'tanaz'); # declaring a hash
       
       # sort using foreach, ascending sort
       foreach my $key (sort {$hash{$a} cmp $hash{$b}} keys %hash)
       {
           print "$key => $value \n";
       }
       
       # sort using foreach, descending sort
       foreach my $key (sort {$hash{$b} cmp $hash{$a}} keys %hash)
       {
           print "$key => $value \n";
       }
       
     
  2. oleber

    oleber New Member

    Joined:
    Apr 23, 2007
    Messages:
    37
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software Developer (Perl, C/C++ and Java)
    Location:
    Hamburg, Germany
    Home Page:
    Correct to:

    ## HASH REFERENCE
    my $hash_ref = {}; # reference to an empty hash


    And add


    dereference:
    my $copy = %$hash_ref;

    and the syntax sugar
    my %hash = (
    23 => 'pradeep',
    26 => 'shabbir',
    34 => 'asha',
    15 => 'tanaz');
     
    shabbir likes this.
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
  4. elec.shabnam

    elec.shabnam New Member

    Joined:
    Feb 13, 2008
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0

Share This Page