Perl hashref iteration example

Created 2003-11-10 / Edited 2010-06-08

Here is an example of iterating over a nested hashref datastructure in Perl.

$domaindb = {
  'thelackthereof.org' => {
    'owner' => 'brock',
    'services' => {
      'web' => 1,
      'dns' => 0,
      'email' => 1
    }
  },
  'brockwilcox.org' => {
    'owner' => 'awwaiid',
    'services' => {
      'web' => 1,
    }
  }
};

foreach $domain (keys %{$domaindb}) {
  print "Domain: $domain\tOwner: $domaindb->{$domain}->{owner}\n";
}

Which outputs:

Domain: thelackthereof.org      Owner: brock
Domain: brockwilcox.org         Owner: awwaiid

Fun, eh?