I would like to know if there is a more efficient way of doing the following, or recommendations on how to make this more robust, smaller, faster, etc... I have a list of up to 1M lines. Each line contains a DEFECT number, followed by up to 30 attributes.
DEFECTID ATTR1 ATTR2 ... ATTR30
I need to be able to select DEFECTIDs that match certain criteria, select a random sample of those, and then add them to a data set.
I currently have a rules hash that I read in from a local file that has the following structure: $hash->{RULENUMBER}->{RULETYPE} = value I want all the rules to be additive ( so if I get 10 defects from rule1, and 8 defects from rule2, my final set would contain 18 defects ). I also need to be able to negate the rule by adding an ! in the value
I have the following algorithm / code working, but I was wondering if it could be better ( one caveat is that I can't generalize and loop through some genereric rule because some are text rules, some are ranges, some are equivalencies, etc. ).
DEFECT: foreach my $defect ( @$list ){ # $summary is my pseudo object, # this line converts each defect line into a hash # whose keys are the attributes desired to be selected my $line = parseLine( $summary, $defect ); if ( ! $line ){ next DEFECT }; RULE: foreach my $rulenum ( keys %$rulelist ){ # one example rule, but there are many.. if ( defined $rulelist->{$rulenum}->{REGION} ){ my $rule = $rulelist->{$rulenum}->{REGION}; if ( $rule =~ s/!// ){ # handles negation if ( $line -> {REGIONID} == $rule ){ next RULE }; } elsif ( $line -> {REGIONID} != $rule) { next RULE }; } } # i create a filtered list in the $summary hash push @{$summary->{FILTEREDLIST}->{$rule}}, $defect; }
The problem is that I have a ton of rules, and looping through each rule for every defect seems to be overkill. I was just wondering if there is a more efficient way of doing this using some kind of parallel approach...
Any help would be greatly appreciated.

Update: Fixed typo pointed out by hbm

In reply to Algorithm To Select Lines Based On Attributes by ~~David~~

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.