Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete empty hash entires?

Tags:

perl

I am trying to figure out how to delete a hash entry that returns a value of {}.

I was working with something like this;

if (ref($snapshots{"ID\:$id"}) eq "{}") {
    print "ID $id hash no snapshots\n";
}

It does not appear to work. Any ideas?

like image 637
ianc1215 Avatar asked May 21 '11 18:05

ianc1215


1 Answers

Given {}, ref will be "HASH" not "{}"

if (ref $snapshots{"ID\:$id"} eq 'HASH' && !scalar keys %{$snapshots{"ID\:$id"}}) {
    delete $snapshots{"ID\:$id"};
}
like image 136
Quentin Avatar answered Oct 01 '22 05:10

Quentin