use strict; use warnings; my (@truth, $c1, $c2, $line, $logic, $v, $c, $cref); my $max = 4; ## Max number of vars $truth[0] = '0 1'; for (1..($max-1)) { ($c1 = $truth[$_-1]) =~ s/(\d+)/0$1/g; ($c2 = $truth[$_-1]) =~ s/(\d+)/1$1/g; $truth[$_] = "$c1 $c2"; } for (0..($max-1)) { my @comb = split(/ /, $truth[$_]); $truth[$_] = \@comb; } for () { my %vars; chomp($line = $_); @_ = $line =~ /(\w+)/g; $v = ''; $c = 0; for (sort @_) { ## Sort and retrieve if ($_ ne $v) { ## unique var names $vars{$v = $_} = $c++; } } print $line . "\n"; print join(' ', sort {$vars{$a} <=> $vars{$b}} keys %vars) . "\n"; $cref = $truth[scalar(keys %vars) - 1]; for (@$cref) { $logic = $line; ## Replace var names with $logic =~ s/(\w+)/substr($_, $vars{$1}, 1)/eg; print $_ . ' ' . eval($logic) . "\n"; } ## logic values and evaluate print "\n"; } __DATA__ ((S0*B)|((!S0)*A)) ((A*B)^C)