#!/usr/bin/perl
########################################################
# How to build multi-dimensional arrays in Perl, then  #
# access hash & array pointers with their data behind. #
# 20070928 frank4dd                                    #
########################################################

print "----------------------------------------------\n";
print "test multi-layered array and hash referencing \n";
print "----------------------------------------------\n";

# create inner hash 1
################################
%myhash1 = ('h1key1', 'h1value1', 'h1key2', 'h1value2', 'h1key3', 'h1value3');

$testkey = "h1key5";
$testvalue = "h1value5";

$myhash1{$testkey} = "$testvalue";

print "test new value: $myhash1{$testkey}\n";

# create inner hash 2
################################
%myhash2 = ('h2key1', 'h2value1', 'h2key2', 'h2value2', 'h2key3', 'h2value3');

# create outer array 1 and add
# pointer to the 2 inner hashes
################################
@myarray1 = (\%myhash1, \%myhash2);

# create outmost array 2 and add
# pointer to the inner array 1
################################
@myarray2 = (\@myarray1);

print "Content of first element in myarray2: ".$myarray2[0]."\n";
print "Content of first element in myarray1: ".$myarray2[0][0]."\n";
print "Content of first element in myhash1: ".$myarray2[0][0]->{'h1key1'}."\n";

print "Add to myhash1: key=h1key4 value=h1value4\n";
$myhash1{'h1key4'} = 'h1value4';

print "Content of last element in myhash1: ".$myarray2[0][0]->{'h1key4'}."\n";

print "----------------------------------------------\n";
print "array and hash pointer dereferencing in loops \n";
print "----------------------------------------------\n";

# dereference pointers in a loop
#################################

$i=1;
$j=1;
$k=1;

foreach $outmost_data (@myarray2) {

  print "Content of $i element in myarray2: ".$outmost_data."\n";
  $i++;
  foreach $outer_data (@{$outmost_data}) {
    print "  Content of $j element in myarray1: ".$outmost_data."\n";
    $j++;
    foreach $inner_key (keys %{$outer_data}) {
      print "    Content of $k element in "
            .$outer_data.": key=".$inner_key
            ." value=".%{$outer_data}->{$inner_key}."\n";
      $k++;
    }
  }
}


print "----------------------------------------------\n";
print "build a list with pointers to multiple arrays \n";
print "----------------------------------------------\n";

# define a new, empty array
################################
@myarray3 = ();

# fill array with sample data
################################
print "myarray3 ".\@myarray3." was created\n";
foreach $count (0,1,2,3) {
  my $key = "h3key".$count;
  my $value = "h3value".$count;
  my %myhash3 = ($key, $value);
  @myarray3[$count] = (\%myhash3);
  print "  Assign myarray3 ".\@myarray3."[$count] hash ".\ %myhash3."\n";
}

print "----------------------------------------------\n";
print "Dereference array and hash to display content \n";
print "----------------------------------------------\n";

$length = @myarray3;
print "myarray3 ".\@myarray3." has $length entries\n";

foreach $entry (@myarray3) {
  print "  Entry: $entry ";
  foreach $key (keys %{$entry}) {
    print "key=$key value=".%{$entry}->{$key}."\n";
  }
}

print "----------------------------------------------\n";
print " End of Perl Pointer Reference example script \n";
print "----------------------------------------------\n";

########################################################################## 
########################### Screeen output ###############################
########################################################################## 
# ----------------------------------------------
# test multi-layered array and hash referencing
# ----------------------------------------------
# test new value: h1value5
# Content of first element in myarray2: ARRAY(0x814f798)
# Content of first element in myarray1: HASH(0x814f600)
# Content of first element in myhash1: h1value1
# Add to myhash1: key=h1key4 value=h1value4
# Content of last element in myhash1: h1value4
# ----------------------------------------------
# array and hash pointer dereferencing in loops
# ----------------------------------------------
# Content of 1 element in myarray2: ARRAY(0x814f798)
#   Content of 1 element in myarray1: ARRAY(0x814f798)
#     Content of 1 element in HASH(0x814f600): key=h1key4 value=h1value4
#     Content of 2 element in HASH(0x814f600): key=h1key2 value=h1value2
#     Content of 3 element in HASH(0x814f600): key=h1key3 value=h1value3
#     Content of 4 element in HASH(0x814f600): key=h1key1 value=h1value1
#     Content of 5 element in HASH(0x814f600): key=h1key5 value=h1value5
#   Content of 2 element in myarray1: ARRAY(0x814f798)
#     Content of 6 element in HASH(0x814f714): key=h2key2 value=h2value2
#     Content of 7 element in HASH(0x814f714): key=h2key3 value=h2value3
#     Content of 8 element in HASH(0x814f714): key=h2key1 value=h2value1
# ----------------------------------------------
# build a list with pointers to multiple arrays
# ----------------------------------------------
# myarray3 ARRAY(0x81701bc) was created
#   Assign myarray3 ARRAY(0x81701bc)[0] hash HASH(0x81702d0)
#   Assign myarray3 ARRAY(0x81701bc)[1] hash HASH(0x814f6e4)
#   Assign myarray3 ARRAY(0x81701bc)[2] hash HASH(0x8170594)
#   Assign myarray3 ARRAY(0x81701bc)[3] hash HASH(0x81705a0)
# ----------------------------------------------
# Dereference array and hash to display content
# ----------------------------------------------
# myarray3 ARRAY(0x81701bc) has 4 entries
#   Entry: HASH(0x81702d0) key=h3key0 value=h3value0
#   Entry: HASH(0x814f6e4) key=h3key1 value=h3value1
#   Entry: HASH(0x8170594) key=h3key2 value=h3value2
#   Entry: HASH(0x81705a0) key=h3key3 value=h3value3
# ----------------------------------------------
#  End of Perl Pointer Reference example script
# ----------------------------------------------
########################################################################## 
