≡ Menu

Perl Examples: Array of Arrays, Hash of Arrays, Hash of Hashes, Stack

Perl developers should understand how to use complex data structures effectively.

In this article, I picked the top 5 useful posts about complex data structures from perlmonks, and gave simple examples on how to use them.

1. Perl Array of Arrays

The following example defines a sample perl array of arrays.

@tgs = (
    ['article series', 'sed & awk', 'troubleshooting', 'vim', 'bash'],
    ['ebooks', 'linux 101', 'vim 101', 'nagios core', 'bash 101' ]
);

To access a single element, for example, to Access 2nd element from the 1st array, do the following:

$tgs[0][1];

Access all the elements one by one as shown below.

print @$_, "\n" foreach ( @tgs );

Read more: How do I make an array of arrays?

2. Perl Hash of Hashes

The following example defines a sample perl hash of hashes.

%tgs = (
    'articles' =>  {
                       'vim' => '20 awesome articles posted',
                       'awk' => '9 awesome articles posted',
                       'sed' => '10 awesome articles posted'
                   },
    'ebooks'   =>  {
                       'linux 101'    => 'Practical Examples to Build a Strong Foundation in Linux',
                       'nagios core'  => 'Monitor Everything, Be Proactive, and Sleep Well'
                   }
);

To access a single element from hash, do the following.

print $tgs{'ebooks'}{'linux 101'};

Read more: How do I make a hash of hashes?

3. Hash of Arrays

The following example defines a sample perl hash of arrays.

%tgs = (
    'top 5' =>  [ 'Best linux OS', 'Best System Monitoring', 'Best Linux Text editors' ],
    '15 example' => [ 'rpm command', 'crontab command', 'Yum command', 'grep command' ],
);

To access all elements one by one, do the following.

foreach my $key ( keys %tgs )  {
    print "Articles in group $key are: ";
    foreach ( @{$tgs{$key}} )  {
        print $_;
    }
}

Read more: How do I make a hash of arrays?

4. Making a Stack

Making a stack is very simple in Perl using arrays with push & pop function.

First, define an array as shown below.

@array = ( 1, 2, 3, 4, 5 );

Stack operation:

push ( @array, 6 ); # pushes the content at the end of array.
pop ( @array ); # gives the top element 6.

Read more: How do I make a stack?

5. Visualize a Complex Perl Data Structure

When you see a code which builds complex data structure, to visualize it better, use Dumper as shown below.

use Data::Dumper;
print Dumper $ref;

The above code snippet will dump the output in a user friendly way as shown below.

$VAR1 = {
          'a' => [
                   {
                     'A' => 1,
                     'B' => 2
                   },
                   {
                     'D' => [
                              4,
                              5,
                              6
                            ],
                     'C' => [
                              1,
                              2,
                              3
                            ]
                   }
                 ]
        };

From the above output, you can clearly tell which is array, which is hash, and the dependencies, etc.

Read more: How can I visualize my complex data structure?

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • ChrisM September 16, 2011, 1:44 pm

    Good reference article. Thanks for adding supporting resources under each concept. For trying to learn *nix and perl on my own I’ve found tgs and pm to be invaluable learning aids. Thanks for the hard work!

  • CRiera April 16, 2012, 7:01 am

    Hi,
    I’ve found your article very useful but I have a question regarding hash of hashes: I have a matrix of Blosum62 values for the 20 amino acid residues, looking like this:

    – A C T R N E …till 20
    A 2 5 8 1 2 4 …
    C 5 2 3 1 5 2 …
    T 8 3 2 2 1 0 ..
    R 1 1 …
    N 2
    E …

    till 20

    So, in total 20×20 = 400 values. I was wondering how to make the script read this from a file and put it into a hash of hashes, since translating this into a structure like :
    %tgs = (
    ‘top 5’ => [ ‘Best linux OS’, ‘Best System Monitoring’, ‘Best Linux Text editors’ ],
    ’15 example’ => [ ‘rpm command’, ‘crontab command’, ‘Yum command’, ‘grep command’ ],
    );
    Would be really hard!
    I want a hash of hashes so that then I can get the blosum value based of two specific residues coming from a list of pair residues. So if A23C, $aa1=A, $aa2=C,
    $hash{$aa1}{$aa2} would give = 5.

    Thank you!

  • Ganesh January 25, 2013, 5:21 am

    Excellent Example…..

  • priyank March 13, 2014, 2:44 am

    you are gave carrcte example