FreeKB - Perl (Scripting) Hash (remove values)
Perl (Scripting) - Hash (remove values)

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 have the following hash. In this example, employee_key contains a value of John Doe.

my %hash = ( 'employee_key' => 'John Doe' );

 

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

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

 

Which shows that the employee key contains a value of John Doe.

$VAR1 = {
           'employee_key' => 'John Doe'
        };

 

The value can be removed from employee_key, like this.

$hash{employee_key} = "";

 

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

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

 

Which shows that the employee key is now empty (contains no value).

$VAR1 = {
           'employee_key' => ''
        };

 


Multidimensional

In this example, name_key is below employee_key.

my %hash = ( 'employee_key' => { 'name_key' => 'John Doe' } );

 

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

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

 

Which shows that name_key contains a value of John Doe.

$VAR1 = {
           'employee_key' => {
                               'name_key' => 'John Doe'
                             }
        };

 

A value can be removed from name_key, like this.

$hash{employee_key}->{name_key} = "";

 

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

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

 

Which shows that the name key now contains no value.

$VAR1 = {
           'employee_key' => {
                               'name_key' => ''
                             }
        };

 



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 b4018 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   |