I'm sorry. I asked a similar question before but lost track of it as I went out of town for a few days. The original question was to parse a custom filter and apply to a dataset. For example, parse this filter:
(AND (OR AUTHOR=John PROFIT=90% AUTHOR=Matt PROFIT=80% ) PUBLISHER=OReilly )
And apply to:
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
to get the relevant results. I think it was BrowserUK who gave me a working solution which was this:
#! 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, 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:
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";
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?

In reply to Filtering some text by legend

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.