in reply to Read conditional strings from a file, search and replace same string in another file in perl

hey mate
is this what you need?
use Inline::Files; %h = qw/and nand or xor/; $f = sub { $fh = shift; while(<$fh>){ chomp; $m = $_ and $a{$m} = {} and next if /^module/; $l = \@{$a{$m}{'in'}} if /^input/; $l = \@{$a{$m}{'out'}} if /^output/; $l = \@{$a{$m}{'op'}} and ($_ = $_) =~s/$&/$h{$&}/ if /^and|^o +r/; $l = \@{$a{$m}{'cond'}{$_}} if /^if/; next if /endmodule/; push @{$l}, $_; } return %a; }; %r = &$f(FILE1); %s = &$f(FILE2); for( keys %r){ print "\n\n$_\n"; print "@{$r{$_}{'in'}}\n"; print "@{$r{$_}{'out'}}\n"; print "$r{$_}{'op'}[0]"; for $x (keys %{$r{$_}{'cond'}}){ if(defined $s{$_}{'cond'}{$x}){ print "\n",join"\n",@{$s{$_}{'cond'}{$x}}; } else { print "\n",join"\n",@{$r{$_}{'cond'}{$x}}; } } print "\nendmodule\n"; } __FILE1__ module and_1 (y,a,b); input a, b; output y; and x1 (y,a,b); if ( a == b) y = 1'b0; else y = 1'bx; if ( a == 1 ) y = 1'b1; else y = 1'b0; endmodule module or_1 (y,a,b); input a, b; output y; or x1 (y,a,b); if ( a == b) y = 1'b0; else y = 1'bx; if ( a == 0 ) y = 1'b0; else y = 1'b1; endmodule __FILE2__ module and_1 (y,a,b); input a, b; output y; and x1 (y,a,b); if ( a == b) y = 1'bx; else y = 1'b0; if ( a == 0 ) y = 1'bx; else y = 1'b1; endmodule module or_1 (y,a,b); input a, b; output y; or x1 (y,a,b); if ( a == b) y = 1'b1; else y = 1'b0; if ( a == 0 ) y = 1'bz; else y = 1'bx; endmodule
  • Comment on Re: Read conditional strings from a file, search and replace same string in another file in perl
  • Download Code

Replies are listed 'Best First'.
Re^2: Read conditional strings from a file, search and replace same string in another file in perl
by Anonymous Monk on Nov 23, 2015 at 05:25 UTC

    Yes, it is working. I am wondering, how beautiful the code is.

    Ver small, effective. Thank you very much.