Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Algorithm To Select Lines Based On Attributes

by perlesque (Initiate)
on Jan 15, 2009 at 18:53 UTC ( [id://736646]=note: print w/replies, xml ) Need Help??


in reply to Algorithm To Select Lines Based On Attributes

David, One method I've used to speed up something similar in the past is to generate a function containing your rule code in advance of your main processing loop. The advantage of this is all of your rule code is compiled and this should reduce the time taken on each DEFECT iteration. Each DEFECT would then simply be passed to your generated rule function. A very simple example would be:
my $rule_code = ""; 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/!// ){ $condition = "!="; } else { $condition = "=="; } # add the rule to the generated rule code $rule_code .= 'if ( $line -> {REGIONID} ' . $condition . "$rule + ) { return \"$rule\" };\n"; } } eval <<EOT; sub match_rule { my \$line = shift; # generated code goes here $rule_code return undef; # we did not match any rules!! }; EOT
Your main loop would then become something like:
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 }; # i create a filtered list in the $summary hash my $rule = match_rule( $line ); if ( defined $rule ) { push @{$summary->{FILTEREDLIST}->{$rule}}, $defect; } }
You may find the generation technique also helpful in making production of your rules simpler/more generic. Kind Regards Frank Perlesque

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found