in reply to (jcwren) RE: Building a byte to test truth table
in thread Building a byte to test truth table
@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.
|
|---|
| 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 |