perlperl-hash

In a hash, can we have three keys combined together that point to one value?


I have a doubt in the below hash declaration:

%metadataHash is a hash

line 1: $metadataHash->{"name"} = $name;
line 2: $metadataHash->{"type"} = $Type;
line 3: $metadataHash->{"student"}{$file}{"math"} = "/$file";
line 4: $metadataHash->{"student"}{$file}{"phy"} = $phy;
line 5: $metadataHash->{"student"}{$file}{"chem"} = $chem;

In line 1 and line 2, it is clear that keys ("name", "type") and values ($name, $Type).

But in line 3,


Solution

  • $metadataHash->{"fastq"}{$file1}{"read1"} is shorthand syntax for $metadataHash->{"fastq"}->{$file1}->{"read1"}.

    It deals with hashes where a value is a reference to another hash.


    To explain with a demonstration:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $foo = {};
    
    $foo->{a}{b}{c} = 1;
    
    use Data::Dumper;
    
    print Dumper($foo);
    

    Gives:

    $VAR1 = {
              'a' => {
                       'b' => {
                                'c' => 1
                              }
                     }
            };