hi monks,
i'm trying to write something that will pass a string as such:
"( foo ) AND ( bar OR boo ) NOT ( far )"
into a hash which i can then use to run some "tests".
so far this is the code i have:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $term = "( foo ) AND ( bar OR boo ) NOT ( far )"; # This regex isn't right my (@matches) = ( $term =~ /( )?(AND|OR|NOT)?( )?(\((.*)\))?/g ); # Testing print $_,"\n" foreach (@matches); # My plan was pull out all the parens and their contents # + the preceeding condition # then modify those result to put into a hash in the same # structure as below. # This is what the results should come out as my %tests = (); $tests{1} = { condition => "", terms => [ "foo" ] }; $tests{2} = { condition => "AND", terms => [ "bar", "OR foo" ] }; $tests{3} = { condition => "NOT", terms => [ "far" ] }; print Dumper(\%tests); exit;
before i went any further i just wanted to see if anyone could suggest a better way of dealing with it?
if not, could anyone help me out with the regex?
cheers,
reagen

In reply to Logical expressions by rsiedl

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.