Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Please find out the mistake in substitution function in the code??

by sumathigokul (Acolyte)
on May 11, 2015 at 13:05 UTC ( #1126313=perlquestion: print w/replies, xml ) Need Help??

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

Hi all..

i have three input files as follows...

file1 is common_modify.vhd which has following lines of codes sum_1_SUM0_0 : XOR3 port map(A => b_c, B => a_c, C => c_c, Y => sum_c); a_pad : INBUF port map(PAD => a, Y => a_c); sum_1_CO0_i : MAJ3 port map(A => a_c, B => c_c, C => b_c, Y => N_5);
file2 is nets.txt which has following lines of codes a_c b_c c_c
file3 is enabled_nets.txt which has following lines of codes a_e b_e c_e

i want my perl script to search for a_c, b_c, c_c (file2) at inputs (A or B or C, not in Y) in common_modify.vhd file and replace them by a_, b_e, c_e (i.e. file3)...Here is the perl code i wrote, but it is not modifying as i want...

open (IN1, "<common_modify.vhd") or die; open (IN2, "<nets.txt") or die; open (IN3, "<enabled_nets.txt") or die; open (OUT, ">output_file.vhd") or die; my @nets = <IN2>; my @enabled_nets = <IN3>; while (<IN1>) { foreach $i (0..$#nets){ print OUT if (s/\=\>\s+$nets[$i]\,/\=\> $enabled_nets[$i]\,/); } }

my output file need to be as follows...

sum_1_SUM0_0 : XOR3 port map(A => b_e, B => a_e, C => c_e, Y => sum_c); a_pad : INBUF port map(PAD => a, Y => a_c); sum_1_CO0_i : MAJ3 port map(A => a_e, B => c_e, C => b_e, Y => N_5);

Please correct the mistakes in the code.. Thank you all for your patience to read my post.....

Replies are listed 'Best First'.
Re: Please find out the mistake in substitution function in the code??
by afoken (Canon) on May 11, 2015 at 13:51 UTC
Re: Please find out the mistake in substitution function in the code??
by GotToBTru (Prior) on May 11, 2015 at 15:42 UTC

    Your print OUT if (...) needs something to print.

    You should work out how you would do this manually in order to form your algorithm. I would suggest building a hash where the values from file2 are the keys, and the corresponding strings from file3 are the values. Then, for each line in file1, loop over the hash keys and replace with hash value if key is found in line.

    Dum Spiro Spero
      Your print OUT if (...) needs something to print.

      print will output the value of  $_ localized in the enclosing  while (<IN1>) { ... } loop.


      Give a man a fish:  <%-(-(-(-<

        I had to look at that again; of course you're right. But only lines for which the substitution is performed will print. I think that is not exactly what is wanted. Perhaps more like:

        while(...) { s/.../.../; print OUT; }

        The lines with recognized codes will be corrected; lines without codes will still be copied to the output.

        Dum Spiro Spero

      Hi GotToBTru...

      Since i want to automate this process and nets.txt and enabled_nets.txt files are all generated automatically, every time I will not know the contents of those files, in such case,. how can i make it as hash.... Do you find any mistake in this code??

        Using the arrays you have already loaded:

        %hash = map { $nets[$_] => $enabled_nets[$_] } 0..$#nets;
        Dum Spiro Spero

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1126313]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2020-11-30 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?