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

by SathiyaMoorthy on September 16, 2011

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?


Share

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

{ 1 comment… read it below or add one }

1 ChrisM September 16, 2011 at 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!

Leave a Comment

Previous post:

Next post: