FreeKB - Perl (Scripting) Hash (define hash)
Perl (Scripting) - Hash (define 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 = [ ];

 

Here are different ways to create an empty hash named "hash".

my %hash;
my %hash = ();

 

Dumper can be used to display the structure of the hash.

use Data::Dumper;
print Dumper \%hash;

 

Which should produce the following.

$VAR1 = {};

 

Keys that contain no values (empty keys) can be created like this.

%hash = (
  foo => "", 
  bar => ""
);

 

Or, like this.

$hash{foo} = undef;
$hash{bar} = undef;

 

Or, like this.

$hash{foo} = "";
$hash{bar} = "";

 

Dumper can be used to display the structure of the hash.

print Dumper \%hash;

 

Which will display the data structure of the hash.

$VAR1 = {
           'foo' => '',
           'bar' => ''
        };

 



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 00f99 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   |