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,
Alex

In reply to Deleting undefined entries from DB_FILE hash by gossamer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.