Here's the guts of the reduction. It's not particularly brilliant, but it seems to work in my testing. If it were not Sunday night, I might get elaborate with error checking or bit vectors, which would really be the way to go with this.
There is room for someone to come along and to reduce my code's verbosity further. Welcommen.
#!/usr/bin/perl -w
use strict;
my $input = "(abdc)(adc-b)";
my @expr;
while ($input =~ s!\(([-a-z,]+)\)!!) {
my @items = split '', $1;
my (@and, @or) = ();
while (@items) {
my $elem = shift @items;
if ($elem eq '-') {
push @or, shift @items;
} else {
push @and, $elem;
}
}
push @expr, [ \@and, \@or ];
}
my $first = shift @expr;
my @output = @{ @$first[0] };
while (my $next = shift @expr) {
my (%keep, %drop);
@keep{@{ @$next[0] }} = ();
@drop{@{ @$next[1] }} = ();
foreach (@output) {
next if (exists $keep{$_});
$_ = undef if (exists $drop{$_});
}
}
my $output = join('', grep{ defined($_) } @output);
print "Result is; $output\n";
Update: I realized early this morning that this only handles the OR case in the second and subsequent input units. Back to the drawing board.
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.