Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

RE: (jcwren) RE: Building a byte to test truth table

by dws (Chancellor)
on Nov 02, 2000 at 08:25 UTC ( [id://39599]=note: print w/replies, xml ) Need Help??


in reply to (jcwren) RE: Building a byte to test truth table
in thread Building a byte to test truth table

Since you're using a hash to hold the object, reusing the keys to map to bit positions might help. For example:
@fields = qw(organization report foo bar baz);
$bit = 1;
foreach $field ( @fields ) {
    $bitmask{$field} = $bit;
    $bit <<= 1;
}

This reduces the code that build $mask to

foreach $field ( @fields ) {
    $mask |= $bit{$field} if $obj{$field} !~ /^\s*$/;
}

The challenging part is converting the static regular expressions to something dynamic. Here's one (untested) thought:

sub testMask {
    my($bits, $on, $off) = @_;
    my($onmask, $testmask) = 0;

    foreach $field ( @$on ) {
        $onmask |= $bit{$field};
        $testmask |= $bit{$field};
    }
    foreach $ field ( @$off ) {
        $testmask |= $bit{$field};
    }
    return ($bits & $testmask) == $onmask;
}

This lets you rewrite the tests as:

moreDatabaseProcessing() unless testMask($mask, \qw(organization), \qw(foo bar));

The up side of this approach is that it's completely position independent. The downside is that it's not resilient against field name typos, though that can be mitigated with some extra tests.

  • Comment on RE: (jcwren) RE: Building a byte to test truth table

Replies are listed 'Best First'.
(PsychoSpunk) RE: RE: (jcwren) RE: Building a byte to test truth table
by PsychoSpunk (Hermit) on Nov 03, 2000 at 05:25 UTC
    After looking at the code samples above and below, dws is the big winner! Thanks to all who gave useful samples, and of course, I took all suggestions and modified to my own look and feel. But the end result is closest to the code above.

    I just wanted to give a final thank you, now that it's all working, and since it seems appropriate to give thanks on big posts like 1000 or 500 or in my case 50.

    ALL HAIL BRAK!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://39599]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-18 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found