in reply to Searching and replacing using perl
A quick search of CPAN didn't turn up anything that looks like it deals with bitwise operators for symbolic algebraic manipulation, but for the simple example you give the following works:
use strict; use warnings; use Math::Algebra::Symbols; my $file = <<FILE; e = a & b; f = e | c; d = ~f; FILE open my $fIn, '<', \$file or die "Can't open file: $!\n"; my %subst; my $last; while (<$fIn>) { chomp; s/;$//; my ($lhs, $rhs) = split /\s*=\s*/, $_, 2; $rhs =~ s/(\w+)/exists $subst{$1} ? "($subst{$1})" : $1/ge; $subst{$lhs} = $rhs; $last = "$lhs = $rhs"; } print $last;
Prints:
d = ~((a & b) | c)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Searching and replacing using perl
by ravimartha (Initiate) on Apr 01, 2012 at 07:18 UTC | |
by GrandFather (Saint) on Apr 01, 2012 at 11:58 UTC |