Reputation: 1231
I have a hash that looks like this
$VAR1 = {
'' => 0,
'example' => 17953878,
'test' => 14504908,
'arbitrary' => 14977444
};
I am printing the hash with the basic
for (keys %hash) {
print "$_ : $hash{$_}";
}
What is the best way to check for the empty key and remove it before printing the hash? Also id like to know how I can check if the key is 'undef' or just an empty string, does it evaluate to false? etc.
Upvotes: 0
Views: 3438
Reputation: 51
If your data is coming from an external source, and you know that empty strings are never valid input, you can just do
delete $hash{''};
before printing.
Upvotes: 3
Reputation: 50677
for (keys %hash) {
length or next; # key is empty string, skip it
# $_ eq "" and next; # explicit comparison to ""
print "$_ : $hash{$_}";
}
Checking with exists
, outside of loop:
print "hash has empty string key\n" if exists($hash{""});
Upvotes: 5