Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to RE: (jcwren) RE: Building a byte to test truth table by dws
in thread Building a byte to test truth table by PsychoSpunk

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-18 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found