FreeKB - Perl (Scripting) Hash (multidimensional hash)
Perl (Scripting) - Hash (multidimensional hash)

In Perl, there are 3 different kinds of hashes.

  • A regular hash, which is defined by the % and ( ) characters - %hash = ( );
  • A reference hash, which is defined with the $ and { } characters = $hash = { };
  • A hash array, which is defined with the % and [ ] characters - %hash = [ ];

 

Let's say you want to create a hash that has two (or more) keys, like this. In this example, the bar key is a child of the foo key.

'foo' {
        'bar' => 'Hello World'
      }

 

Here is how you can create the multidimensional keys and value.

my %hash = ( 'foo' => { 'bar' => 'Hello World' } );

 

Or like this.

my $hash{foo}->{bar} = "Hello World";

 

Dumper can be used to print the hash.

print Dumper \%hash;

 

Which will produce the following.

$VAR1 = {
          'foo' => {
                     'bar' => 'Hello World'
                   }
        };

 

Or to print the value of the bar key.

print $hash{foo}->{bar};

 

Which will produce the following.

Hello World

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter e1cbd in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |