anirbanphys has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I don't have any idea in Perl, but I need to do

something, so I have started learning PERL.

The requirement for me is given below.

I have 2 files.

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

====================

Now I need to parse FILE1, FILE2. Get some STRINGS from

FILE2 and replace them in FILE1 and write it down in FILE3.

The followings steps need to do.

Search for keyword "module". Both FILE1 and FILE2 has module

and_1, or_1. FILE_1 => "module" must be mapped with FILE_2

=> "module".

Now "and" rplaced by "nand" and "or" replaced by "xor" ( I

was able to do this),

After then it will search "if" keyword and the corresponding

condition in FILE1 and will map that from FILE2 and what

ever next line in FILE2 has will be replaced by that in

FILE1. So FILE3 will be look like this.

FILE3

===========

module and_1 (y,a,b); //not changing anything input a, b; output y; nand x1 (y,a,b); //and changed to nand if ( a == b) // searching for "if ( a == b)" y = 1'bx; // replaced condition "y = 1'b0;" in FILE1 with "y + = 1'bx;" from FILE2 else y = 1'b0; // similarly from the upper if ( a == 1 ) y = 1'bx; else y = 1'b1; endmodule module or_1 (y,a,b); input a, b; output y; xor 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

===========================

Your help really needed for me.

Thanks

  • Comment on Read conditional strings from a file, search and replace same string in another file in perl
  • Select or Download Code

Replies are listed 'Best First'.
Re: Read conditional strings from a file, search and replace same string in another file in perl (Verilog)
by toolic (Bishop) on Nov 18, 2015 at 12:16 UTC
Re: Read conditional strings from a file, search and replace same string in another file in perl
by Anonymous Monk on Nov 18, 2015 at 11:11 UTC
Re: Read conditional strings from a file, search and replace same string in another file in perl
by Lennotoecom (Pilgrim) on Nov 19, 2015 at 07:09 UTC
    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

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

      Ver small, effective. Thank you very much.