Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to delete empty hash element

Status
Not open for further replies.

stevio

Vendor
Jul 24, 2002
78
AU
Using Data::Dumper module I can see an empty hash element, in this case $VAR1/$VAR2

Code:
$VAR1 = '';
$VAR2 = {};
$VAR3 = '5';
$VAR4 = '7';

etc

How do I check and remove empty hash elements? Do I use undef?


 
I've looked at the delete function, but can't work out how to search through a hash and delete empty key=>values
 
Your [tt]$VAR1,[/tt] etc are not hash elements, so I'm not sure to understand what you mean. Also you can have only a single empty key value in a hash, corresponding to the string of zero length [tt]''[/tt]. This is easy to delete with
[tt]for(keys%hash{delete$hash{$_}unless$_}[/tt]
Note however that this will also delete the key '0'!
I can't see at the moment a generalized method for detecting whether a hash value is 'empty' as you say, as it depends on what you mean by empty. This
[tt]for(keys%hash{delete$hash{$_}unless$hash{$_}}[/tt]
will delete all values corresponding to false, that is the undefined scalar, a string of zero length, the string (or number) '0' (though not the string '00') and possibly a few others. However if, as in your example, you have references to arrays or hashes with zero elements, these will not be deleted, in this case you should test against the number of elements in them. There are also a few other cases to consider: references to subs, possibly elements defined through constants, and...?

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
prex1, maybe my terminology and assumptions were wrong, but I thought that by using Data::Dumper and the result was

Code:
$VAR1 = '';
$VAR2 = {};

that this meant that the key=>value pair was empty or NULL or zero length as you call it.

Let me ask a diff question - what does the above tell you about a key->value pair?

 
use Data::Dumper like this:

print Dumper \%hash

or whatever your data is. Give it a reference to your data that way you can see the data structure.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Never used that module, but I understand it is informative only, so you won't use it for changing your data (such as deleting a key-value pair).
Concerning your last example, if that is the output of [tt]print Dumper %hash;[/tt] , it tells you that [tt]%hash;[/tt] has one key-value pair in it: the key is the string of zero length and the value is a reference to a hash with zero key-value pairs in it.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
This reproduces the output that stevio posted:

Code:
%hash = ( '' => {}, 5 => 7);
use Data::Dumper;
print Dumper %hash;

passing a reference to the data:

Code:
%hash = ( '' => {}, 5 => 7);
use Data::Dumper;
print Dumper \%hash;

outputs something a bit more useful:

Code:
$VAR1 = {
          '' => {},
          '5' => 7
        };

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top