Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Algorithm To Select Lines Based On Attributes

by jethro (Monsignor)
on Jan 15, 2009 at 18:00 UTC ( [id://736618]=note: print w/replies, xml ) Need Help??


in reply to Algorithm To Select Lines Based On Attributes

This looks like you might be able to generate some sort of parallel comparision. If you wanted only lines where all rules matched it might even have been possible to use a combination of anding strings together and a regex match, used twice for non-negated and negated.

But since you want or, the fastest way I came up is to construct the comparision code into a string and eval that for each line. Sort of compiling the search pattern.

For example, if you have two rules that compare attr2 with 'n' and attr4 with '!x', then you could construct the following code string:

return 1 if ($a[2] eq 'n' or $a[4] neq 'x'); return 0;
Now just eval this for each line, having your lines split into @a first. If you need to know which rules matched, construct this instead:

my @return=(); if ($a[2] eq 'n') { push @return,2; } if ($a[2] neq 'x') { push @return,4; } return @return;

The only thing left is to eval your generated code for each line and check the return value of the eval

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (10)
As of 2024-04-18 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found