And apply to:(AND (OR AUTHOR=John PROFIT=90% AUTHOR=Matt PROFIT=80% ) PUBLISHER=OReilly )
to get the relevant results. I think it was BrowserUK who gave me a working solution which was this:Page 1 AUTHOR: John PROFIT: 20% PUBLISHER: TMH BOOK: OPERATING SYSTEMS Page 2 AUTHOR: John PROFIT: 90% PUBLISHER: OREILLY BOOK: ALGORITHMS Page 3 AUTHOR: Matt PROFIT: 80% PUBLISHER: TMH BOOK: COMPUTER NETWORKS Page 4 AUTHOR: Matt PROFIT: 80% PUBLISHER: OREILLY BOOK: COMMUNICATION SYSTEMS
Now, the problem is I have to use the same filter in a code that I've already written for another format. I have some lines in my code that print the fields:#! perl -slw use strict; ## Convert query syntax to evalable Perl code sub buildQuery { local $_ = uc shift; while( m[ \( (?: AND | OR ) | != | = ]x ) { s< ( [^!=()\s]+ ) ( != | = ) ( [^()\s]+ ) >{ my $op = ( $2 eq '!=' ? 'ne' : 'eq' ); "[\$\L$1\E $op '$3']"; }xge; s< \( \s* ( AND ) \s+ ( \[ [^()]+ \] ) \s+ ( \[ [^()]+ \] ) \s +* \) > { "[$2 && $3]" }xge; s< \( \s* ( OR ) \s+ ( \[ [^()]+ \] ) \s+ ( \[ [^()]+ \] ) \s* + \) > { "[$2 || $3]" }xge; } tr/[]/()/; return $_; } ## Read data and UPPER case my $data = do{ local $/; uc <DATA> }; ## Some variables my( $author, $profit, $publisher, $book ); ## And a regex to populate them from each record my $re = qr[ PAGE \s+ \d+ \s+ AUTHOR: \s+ ( [^\n]+ ) (?{ $author = $^N }) \s+ PROFIT: \s+ ( [^\n]+ ) (?{ $profit = $^N }) \s+ PUBLISHER: \s+ ( [^\n]+ ) (?{ $publisher = $^N }) \s+ BOOK: \s+ ( [^\n]+ ) (?{ $book = $^N }) \s+ ]x; ## Covert the query NOTE: (AND ) syntax is required ## where example used implicit AND my $query = buildQuery( <<EOQ ); (AND (OR (AND AUTHOR=John PROFIT=90% ) (AND AUTHOR=Matt PROFIT=80% ) ) PUBLISHER=OReilly ) EOQ print "\nQuery: $query\n"; ## Test the condition and print the record if it matches ## For each record eval "$query" and print $1 while $data =~ m[ ( $re ) ]xg; ## Same again for another query ## Note != also accepted. my $query2 = buildQuery( <<EOQ ); (OR (AND AUTHOR=John PROFIT!=90% ) (AND AUTHOR=Matt PUBLISHER!=OReilly ) ) EOQ print "\nQuery: $query2\n"; eval "$query2" and print $1 while $data =~ m[ ( $re ) ]xg; __DATA__ Page 1 AUTHOR: John PROFIT: 20% PUBLISHER: TMH BOOK: OPERATING SYSTEMS Page 2 AUTHOR: John PROFIT: 90% PUBLISHER: OREILLY BOOK: ALGORITHMS Page 3 AUTHOR: Matt PROFIT: 80% PUBLISHER: TMH BOOK: COMPUTER NETWORKS Page 4 AUTHOR: Matt PROFIT: 80% PUBLISHER: OREILLY BOOK: COMMUNICATION SYSTEMS
Now, I have to print all the fields only if the filter condition matches but I am unable to get it right on how to use the filter with this data. Can someone help me out please?print $db_title."\n"; print $db_source."\n"; print $db_length."\n"; print $db_author."\n"; print $db_body."\n"; print $db_language."\n"; print $db_subject."\n"; print $db_datepub."\n"; print $db_loaddate."\n"; print "\n\n\n";
In reply to Filtering some text by legend
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |