Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I have finally solved this problem, after months of cheating and using File::Glob to do the hard work for me. (I ran into string-length limitations with File::Glob which prevented it from working for very long boolean statements.)

Here is my Perl solution, translated from (obligatory shudder) PHP. Hopefully the code speaks for itself and is not in need of much documentation for explaining its logic. NOTE: "not" nodes are skipped, because the point of this function is to come up with lists of terms that will match the logic.

#!/usr/bin/perl -l use strict; use warnings; my $tree = ['and', ['or', 1, 2], ['not', 3], ['or', 4, 5, 6]]; my $opts = options($tree); for my $o (@$opts) { print "OPTION: ", join(", ", @$o); } sub options { my ($tree, $opts) = @_; if (! $opts) { $opts = [ [] ]; options($tree, $opts); return $opts; } else { my $op = $tree->[0]; if ($op eq 'and') { for my $val (@$tree[1 .. $#$tree]) { if (ref($val) eq 'ARRAY' and ($val->[0] eq 'and' or $val->[0] +eq 'or' or $val->[0] eq 'not')) { my $subtree = options($val); my @new; for my $branch (@$subtree) { for my $o (@$opts) { push @new, [ @$o, @$branch ]; } } @$opts = @new; } else { for my $o (@$opts) { push @$o, $val; } } } } elsif ($op eq 'or') { my @new; for my $val (@$tree[1 .. $#$tree]) { if (ref($val) eq 'ARRAY' and ($val->[0] eq 'and' or $val->[0] +eq 'or' or $val->[0] eq 'not')) { my $subtree = options($val); for my $branch (@$subtree) { for my $o (@$opts) { push @new, [@$o, @$branch]; } } } else { for my $o (@$opts) { push @new, [@$o, $val]; } } } @$opts = @new; } } }
Jeffrey Pinyan (Perl, PHP ugh, JavaScript) — @PrayingTheMass
Melius servire volo
Catholic Liturgy

In reply to Re: Walking a boolean tree to produce matching inputs by japhy
in thread Walking a boolean tree to produce matching inputs by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2022-05-21 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?