gossamer has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I've been given a set of DB_FILE hashes that apparently have null, or undefined, entries that I can't figure out how to properly handle. I'd like to just delete the entries from the database after it's been created with the improper entries.
However, it doesn't seem that they are actually being deleted.
These are hashes of amavisd quarantine spam and virus files that include the type, a key, and the date from the filename, such as virus-b2291ce2b21d1cf6f6af68bd3e355505-20120112T115534-10090-04-3. This is then split into 7 pieces and tab-delimited and stored in the hash.
Apparently either all files don't have exactly seven fields, or there are otherwise some null entries made, which causes my routies to choke and die with "undefined" errors.
The problem is that the delete($hash{$key}) doesn't seem to actually be deleting the hash, because if I run the program again, it finds the same undefined entry.
push @myhosts, 'mymail01'; push @myhosts, 'mymail02'; foreach my $mypc (@myhosts) { for (my $i = 0; $i < 256; $i++) { my $bucket = sprintf('%02x', $i); my $file = sprintf('%s/%s/%02x.db', $qdir, $mypc, $i); tie (my %hash, 'DB_File', $file, O_RDWR, 0600) || next; foreach my $key ( keys %hash ) { if (!defined $hash{$key}) { print "$key not defined\n"; delete($hash{$key}); untie %hash; exit 1; } # $type\t$d$t\t$size\t$from\t$to\t$subj\t$score my @tmp = split /\t/, $hash{$key}, 7; my $type = $tmp[0]; my $dt = $tmp[1]; my ($year, $month, $day) = $dt =~ m|(\d{4})(\d{2})(\d{2})T.*|; printf("bucket: %s\ttype: %s\t%s-%s-%s\t%s\n",$bucket,$type,$ye +ar,$month,$day,$dt) } untie %hash;
Running the program where it produces the "undefined" error looks like this:
bucket: aa type: spam 2012-07-07 20120707T204829 spam-aa0b162b5118763a9f053d1412f9f0b1-20120705T082627-27218-15.gz not +defined Use of uninitialized value within %hash in concatenation (.) or string + at ./list_hash.pl line 66.
The "bucket" line above is the values from the previous hash entry which is successfully defined. It only makes it to the printf if $hash{$key} is defined.
Is the issue that the $hash{$key} that I'm using or referring to doesn't actually match the key I'm trying to delete?
Am I properly determining if a value is undefined? Is (!defined $hash{$key}) the proper way? I've searched quite a bit for the proper way to determine if a value is undefined, and haven't really been successful.
Thanks,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Deleting undefined entries from DB_FILE hash
by jwkrahn (Abbot) on Jul 23, 2012 at 06:07 UTC | |
by gossamer (Sexton) on Jul 23, 2012 at 19:07 UTC | |
by 2teez (Vicar) on Jul 24, 2012 at 00:45 UTC | |
by gossamer (Sexton) on Jul 24, 2012 at 02:34 UTC | |
|
Re: Deleting undefined entries from DB_FILE hash
by 2teez (Vicar) on Jul 23, 2012 at 02:25 UTC | |
by gossamer (Sexton) on Jul 23, 2012 at 02:49 UTC | |
by 2teez (Vicar) on Jul 23, 2012 at 03:19 UTC | |
by gossamer (Sexton) on Jul 23, 2012 at 13:07 UTC | |
|
Re: Deleting undefined entries from DB_FILE hash
by Athanasius (Archbishop) on Jul 23, 2012 at 02:55 UTC |