Lesson 21: References and multidimensional data structures


References are only addresses. Multidimension data structures are just hashes and arrays inside of hashes and arrays.

Creating references

  • References are pointers to data, or the address of the data
  • All data has an address in memory
  • Humans have no need to know the address
  • References are useful because they are a scalar variable.
  • Arrays and hashes are not scalar variables.
  • The only kind of data that you can store in an array or hash is scalar
  • We can now store hashes and arrays in hashes and arrays by storing the address
  • Retrieve the address with '\'
1
2
3
my @array = qw (this sentence will be turned into a list);
my $address = \@array;
print "$address\n";

Output:

%% ./references.pl
ARRAY(0x1008156c0

Storing references

Now that we have a way to retrieve the address we can store an array or a hash in an array or hash.

1
2
3
4
5
6
my @array = qw (this sentence will be turned into a list);
my $address = \@array;
 
my %hash;
$hash{'array'} = $address;
print Dumper \%hash;

Output:

%% ./references.pl
$VAR1 = {
          'array' => [
                       'this',
                       'sentence',
                       'will',
                       'be',
                       'turned',
                       'into',
                       'a',
                       'list'
                     ]
        };

If @array is changed the hash also changes, because the hash contains only the address of the array, not a copy of the array

1
2
3
pop @array;
print "After pop\n";
print Dumper \%hash;

Output:

%% ./references.pl
After pop
$VAR1 = {
          'array' => [
                       'this',
                       'sentence',
                       'will',
                       'be',
                       'turned',
                       'into',
                       'a'
                     ]
        };
  • You do not always need to store the address of a data structure in a variable.
  • You can create an anonymous array or hash on the fly. It is anonymous because it is unnamed.
  • notice the [ ] in the anonymous array assignment, we use these with array indicies
  • notice the {} in the anonymous hash assignment, we use these with hash indicies
1
2
3
4
5
6
7
8
9
# anonymous array in a hash
$hash{'array2'} = ['this','is','an','anonymous','array'];
 
# anonymous hash in a hash
$hash{'hash'} = { 'key1' => 'value1' ,
                  'key2' => 'value2'
                 };
 
print Dumper \%hash;

Output:

%% ./references.pl
$VAR1 = {
          'hash' => {
                      'key2' => 'value2',
                      'key1' => 'value1'
                    },
          'array' => [
                       'this',
                       'sentence',
                       'will',
                       'be',
                       'turned',
                       'into',
                       'a'
                     ],
          'array2' => [
                        'this',
                        'is',
                        'an',
                        'annonymous',
                        'array'
                      ]
        };

Retrieving all or pieces of the data from references

Whole chunks of data or pieces of data can be retrieved from our multidimensional structures by using the address.

steps to dereference (retrieve data from address)

  1. Get your address, or reference: ADDRESS
  2. Wrap your address, or reference in {}: {ADDRESS}
  3. Put the symbol of the data type out front @: @{ADDRESS}
1
2
3
4
my @array = qw (this sentence will be turned into a list);
my $address = \@array;
print "$address\n";
print "array from dereferenced reference:\n @{$address}\n";

Output:

%% ./references.pl
ARRAY(0x1008156c0)
array from dereferenced reference:
this sentence will be turned into a list
  • You do not always have to use '\' to get the address.
  • If the value of a key in a hash is an address, then you just need to retrieve the value to get the address
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$hash{'array2'} = ['this','is','an','annonymous','array'];
 
# get address
# my $address = $hash{'array2'}
# my @copy_of_array = @ { $address };
 
my @copy_of_array = @ { $hash{'array2'} };
 
my $second_element =  $ { $hash{'array2'} } [2];
print "[2] = $second_element\n";
 
$hash{'hash'} = { 'key1' => 'value1' ,
                  'key2' => 'value2'
                 };
 
# get address
# $address = $hash{'hash'};
# my %inner_hash = % { $address };
 
my %inner_hash = % { $hash{'hash'} };
my $value = $ { $hash{'hash'} } {'key2'};
print "key2 = $value\n";

Output:

%% references.pl
[2] = an
key2 = value2

Exercises

  1. Create a script in which you store arrays in a hash.
  2. Print the second element of one of your arrays
  3. use a foreach loops to print every key and value of your hash, as well as each element of your array.

Print Friendly

3 thoughts on “Lesson 21: References and multidimensional data structures

  1. Hi Sofia,
    I should say:

    notice the {} in the anonymous hash assignment, we use these with HASH indices
    instead of
    notice the {} in the anonymous hash assignment, we use these with array indicies

    Cheers,
    Bert

Leave a Reply

Your email address will not be published. Required fields are marked *