Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Selective lines updation from one file (file1) to another (file2) without changing the rest of the sections to a new fine($dir/file2)

by anirbanphys (Beadle)
on May 26, 2016 at 18:20 UTC ( [id://1164210]=perlquestion: print w/replies, xml ) Need Help??

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

Hello All,

I am trying to write a perl code which will do the following task.

The code will read two .lib file, ref_lib.lib and org_lib.lib. After doing the operations it will dump final_lib/org_lib.lib.

From ref_lib.lib it will only read the lines from "cell(" ( everything in between) to "leakage_power". And whatever it is reading from ref_lib.lib will be stored in a variable, say $attr_org.

Similarly it will also read org_lib.lib. Now the code will dump everything, from line1 of org_lib.lib to final_lib/org_lib.lib, except the lines in between "cell(" and "leakage_power". This lines will be replaced by $attr_org.

The structure of final_lib/org_lib.lib are also given below.

I tried to write the code but can not understand where it is going wrong.

Can you folks please let me know what change should be in the code?

# Staring of perl code attr_update.pl my $ref_lib = $ARGV[0]; my $org_lib = $ARGV[1]; my $cell_flag = 0; my $lkg_flag = 0; my $attr_org; my $cell_name_ref; my $cell_name_org; system "rm -rf final_lib"; system "mkdir final_lib"; if ($#ARGV !=1) { print STDERR "Usage of script: perl attr_update.pl <REF_LIB> <ORG_LI +B> \n" ; } open (REFLIB,"<","$ref_lib") || die "Can not open REF_LIB" ; open (ORGLIB,"<","$org_lib") || die "Can not open ORG_LIB" ; open (FINALLIB,">","final_lib/$org_lib") || die "Can not open ORG_LIB" + ; while (my $line = <REFLIB>) { chomp($line); #print "$line \n"; if ($line=~m/^\s*cell\(.*/g){ $cell_flag = 1; $lkg_flag = 0; $cell_name_ref = $line; #print "OK 1\n"; } #<STDIN> if ($line=~m/^\s*leakage_power\(\)/g){ $cell_flag = 0; $lkg_flag = 1; } if (($cell_flag ==1) && ($lkg_flag == 0)){ $attr_org = $line; #print "$attr_org \n"; } while (my $line_2 = <ORGLIB> ){ chomp ($line_2); if ($line_2=~m/^\s*cell\(.*/g){ $cell_name_org = $line_2; } if ($cell_name_ref == $cell_name_org){ print FINALLIB "$attr_org \n"; } else { print FINALLIB "$line_2 \n"; } } }
##Staring of ref_lib.lib library_for_dev_47nm_proj { cell(and2_f2) { area : 434.7; cell_type : combo; cell_function : A * B; timing_const : NIL; clock_present : NIL; leakage_power() { power_pin : "VDD" ; when : "!A&!B" ; value : "0.000047" ; } pin(A1) { direction : input; capacitance : 2.141; } pin(Y) { direction : output; function : "A1 * B1"; } } cell(nand2_f2) { area : 359.1; cell_type : combo; cell_function :(A1 * B1)\u201; timing_const : NIL; clock_present : NIL; leakage_power() { power_pin : "VDD" ; when : "!A&B" ; value : "0.00057" ; } pin(A1) { direction : input; capacitance : 12.547; } pin(B1) { direction : input; capacitance : 12.259; } pin(O) { direction : output; function : "(A1 * B1)\u2019"; } } cell(dfr_f5) { area : 4819.5; cell_type : seq; cell_function : d=!q; timing_const : YES; clock_present : YES; leakage_power() { power_pin : "VDD" ; when : "D" ; value : "5.00057" ; } pin(Q) { direction : output; function : "IQ"; } } }
##Staring of org_lib.lib library_for_dev_47nm_proj { lu_table_template(t4x3) { variable_1: total_output_net_capacitance ; variable_2: input_net_transistion ; index_1 {\u201c5, 20, 60, 200\u201d} ; index_2 {\u201c0.01, 0.1, 2.0\u201d} ; } cell(and2_f2) { cell_type : combo; cell_function : A * B; timing_const : NIL; clock_present : NIL; leakage_power() { power_pin : "VDD" ; when : "!A&!B" ; value : "0.000047" ; } pin(A1) { direction : input; capacitance : 2.141; } pin(B1) { direction : input; capacitance : 1.948; } pin(Y) { direction : output; function : "A1 * B1"; } } cell(nand2_f2) { area : 359.1; cell_type : combo; cell_function : (A1 * B1)\u201; clock_present : NIL; leakage_power() { power_pin : "VDD" ; when : "!A&B" ; value : "0.00057" ; } pin(A1) { direction : input; capacitance : 12.547; } pin(B1) { direction : input; capacitance : 12.259; } pin(O) { direction : output; function : "(A1 * B1)\u2019"; } } cell(dfr_f5) { area : 4819.5; timing_const : YES; clock_present : YES; leakage_power() { power_pin : "VDD" ; when : "D" ; value : "5.00057" ; } ff(IQ,IQN) { next_state : "DATA1"; clocked_on : "CLK2\u2019"; clear : "RST3\u2019"; } pin(DATA1) { direction : input; capacitance : 51.289; } pin(CLK2) { direction : input; capacitance : 52.305; } pin(RST3) { direction : input; capacitance : 28.602; } pin(Q) { direction : output; function : "IQ"; } } }
##Staring of final_lib/org_lib.lib library_for_dev_47nm_proj { lu_table_template(t4x3) { variable_1: total_output_net_capacitance ; variable_2: input_net_transistion ; index_1 {\u201c5, 20, 60, 200\u201d} ; index_2 {\u201c0.01, 0.1, 2.0\u201d} ; } cell(and2_f2) { area : 434.7; cell_type : combo; cell_function : A * B; timing_const : NIL; clock_present : NIL; leakage_power() { power_pin : "VDD" ; when : "!A&!B" ; value : "0.000047" ; } pin(A1) { direction : input; capacitance : 2.141; } pin(B1) { direction : input; capacitance : 1.948; } pin(Y) { direction : output; function : "A1 * B1"; } } cell(nand2_f2) { area : 359.1; cell_type : combo; cell_function : (A1 * B1)\u201; timing_const : NIL; clock_present : NIL; leakage_power() { power_pin : "VDD" ; when : "!A&B" ; value : "0.00057" ; } pin(A1) { direction : input; capacitance : 12.547; } pin(B1) { direction : input; capacitance : 12.259; } pin(O) { direction : output; function : "(A1 * B1)\u2019"; } } cell(dfr_f5) { area : 4819.5; cell_type : seq; cell_function : d=!q; timing_const : YES; clock_present : YES; leakage_power() { power_pin : "VDD" ; when : "D" ; value : "5.00057" ; } ff(IQ,IQN) { next_state : "DATA1"; clocked_on : "CLK2\u2019"; clear : "RST3\u2019"; } pin(DATA1) { direction : input; capacitance : 51.289; } pin(CLK2) { direction : input; capacitance : 52.305; } pin(RST3) { direction : input; capacitance : 28.602; } pin(Q) { direction : output; function : "IQ"; } } }

Replies are listed 'Best First'.
Re: Selective lines updation from one file (file1) to another (file2) without changing the rest of the sections to a new fine($dir/file2)
by graff (Chancellor) on May 27, 2016 at 04:28 UTC
    Here's how I would summarize the most obvious problem with the code as posted:
    open( REFLIB, ... ) open( ORGLIB, ... ) while (<REFLIB>) { ... while (<ORGLIB>) { .... } }
    Having the second while loop nested like that means that you read to the end of the second input file immediately upon reading the first line of the first input file. In other words, by the time you get to the second line of input from REFLIB, there's no more data available to be read from ORGLIB because that file handle has reached end-of-file.

    So you should do everything you need to do with all of the content from one file before you open the other file. I'm not sure whether it matters which file you handle first, but whatever you need from the first file must be stored in a suitable data structure, which you can then work with as you read through the second file.

    Another big problem with your approach is that you're using line-oriented operations to deal with multi-line structured data. This makes it way too easy to create output that is badly structured. That's why you should be very grateful for toolic's question about the nature of your data -- and the link he provided to some relevant CPAN modules for handling data files of that type.

    You should seriously consider taking advantage of the work that others have already done in dealing with the "synopsis liberty" data format. I looked at the man page for Parse::Liberty, and I'll admit that it isn't clear to me how it would be applied to the task you describe. (It's unfortunate that the documentation isn't better.)

    Anyway, if that (or some other "liberty"-related) module doesn't work for you, you'll at least have to pay closer attention to the structure of the input -- at least try to use something that involves reading bracketed data, so you can keep track of the structure.

    (minor updates to grammar and punctuation)

    One other update: Please consider starting your code with some documentation (the POD formatting is easy to learn and use). Start over from scratch, but instead of writing the perl code first, describe what the code will do - what the procedure will be - as simply and clearly as possible (use your native language, if that will help). The description can look like pseudo-code, or a step-wise recipe. When you think you have it worked out clearly, then write the actual perl code to carry out the recipe. I always write code that way.

Re: Selective lines updation from one file (file1) to another (file2) without changing the rest of the sections to a new fine($dir/file2)
by toolic (Bishop) on May 26, 2016 at 18:38 UTC

      Yes, this is a sample synopsys liberty file. And I need to update the attributes from ref_lib to original_lib in another directory.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-28 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found