I played around with RecDescent a bit last night after posting. I think for the types of things I'm trying to express it may be more than I need (though it will definately do it) I have pretty strict control over how these things get added (adding clauses via a web form) so I don't need to deal with the vaugeries of human typed input. I came up with this which appears to be working for me.
Just a fragment that's going to be inserted into a larger bit of code, no error handling/strict etc. Just a poc at this point.
#!/usr/bin/perl
+
+
use Data::Dumper;
my $clauses;
push(@$clauses, ["==","a","b"]);
push(@$clauses, "and");
push(@$clauses, ["==","c","d"]);
push(@$clauses, "or");
push(@$clauses, ["==","e","f"]);
push(@$clauses, "and");
push(@$clauses, ["==","g","h"]);
$Data::Dumper::Indent = 0;
+
+
my $list = BuildList($clauses);
print Dumper($list). "\n";
sub BuildList($)
{
my $clauses = shift();
my $list;
while(my $clause = shift(@$clauses))
{
if($clause eq "and")
{
$list = ['AND',[$list], [shift(@$clauses)]];
}
elsif($clause eq "or")
{
$list = ["OR",[$list, BuildList($clauses)]];
}
else
{
$list = $clause
}
}
return($list);
}
This gives me
['OR',[['AND',[['==','a','b']],[['==','c','d']]],['AND',[['==','e','f'
+]],[['==','g','h']]]]]
Thanks for all the input.
Update: It's also important to note, that I don't really care about what's in each clause. The clauses are going to be pretty tightly controlled by the creation mechanisim.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.