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

Hello Experts,

I am facing one issue with a mistake in the given code (main_code.pl). I have two input files.

File_1=> main_file

File-2=> input_to_file1

And File_3 is the output file.

main_code.pl is reading File_1 and File_2. The code is getting two information from File_1. Pin information and the corresponding direction information. For the same pin match from File_2, it is adding (here tried with search and replace) one more line with the corresponding value to a new file.

But the problem here is, only the last value is getting updated. I did a very big mistake. Would be great if someone please point me the mistake.

##### main_code.pl my $line4; my $pin_name; my $out_pin; my $outdir_pin; my $flag_pin=0; my $csvoutpin; my $max_tran_val; open (FINAL1,"<","File_2") || die "Can not open Final_output.csv File" +; while (my $finalline1 = <FINAL1>) { chomp $finalline1; print "$finalline1\n"; @finalline1=split(',',$finalline1); $csvoutpin=$finalline1[0]; $max_tran_val=$finalline1[1]; # print "max_tran_val :: $max_tran_val \n"; # print "csvoutpin :: $csvoutpin \n"; open (OUTFILE,">","File_3") || die "Can not open FINAL LIB File"; open (IFILE,"<","File_1") || die "Can not open Input LIB File"; while ( $line4 = <IFILE>) { chomp $line4; if ($line4 =~m/^\s*pin\s*\(\"(.*)\"\)/g || $line4 =~m/^\s*pin\(( +.*)\)/g) { $pin_name = $1; chomp $pin_name; } if ($line4 =~m/^\s*direction\s*:\s*output/g) { $out_pin = $pin_name; $outdir_pin = $line4; $flag_pin =1; } if (($csvoutpin eq $out_pin) && ($flag_pin == 1)) { $line4 =~ s/$outdir_pin/$outdir_pin\n max_transition :$max +_tran_val /g ; $flag_pin=0; } print OUTFILE "$line4 \n"; } } close OUTFILE; close FINAL1; close IFILE; ##### end of main_code.pl

File_1

cell(main_input){ dont_do : yes ; dont_edit : yes ; pin ("A") { direction : input ; } pin ("B") { direction : input ; } pin ("C") { direction : output ; } pin ("D") { direction : output ; } }

File_2

C,10 D,20

Ideal File_3 should be like this

cell(main_input){ dont_do : yes ; dont_edit : yes ; pin ("A") { direction : input ; } pin ("B") { direction : input ; } pin ("C") { direction : output ; max_transition : 10; } pin ("D") { direction : output ; max_transition : 20; } }

But I am getting

cell(main_input){ dont_do : yes ; dont_edit : yes ; pin ("A") { direction : input ; } pin ("B") { direction : input ; } pin ("C") { direction : output ; } pin ("D") { direction : output ; max_transition : 20; } }

Replies are listed 'Best First'.
Re: Last value update is only happening instead of all.
by pryrt (Abbot) on Aug 25, 2018 at 21:36 UTC

    I believe the culprit is that you are opening File_3 in overwite > for every line of File_2. Either switch to append >>, or (better) move the File_23 opening to outside the loop. (On my phone, so no examples, sorry).

    Oh, and use warnings; use strict;, and lexical filehandles (see open)

    edit: fix file number

      Thank you pryrt for the suggestion. I will use those suggested options at the beginning of any code. I tried to write File_3 in append mode, also moved File_3 outside of the loop. Both the cases is not working for me :( . In one case the whole input i.e. File_1 repeating twice. In another case update is coming both for the pin, but the whole input repeats twice.

      My humble request, please run the code once at your end.

      With advance thanks

      Anirban

        Read File_2 and store the values in a hash. You can then copy File_1 inserting the new lines as required. For example

        #!/usr/bin/perl use strict; use warnings; #use Data::Dumper; open FINAL1,'<','File_2.txt' or die "Can not open Input File_2 : $!"; my %max_trans = (); while (<FINAL1>){ next unless /\S/; # skip blanks chomp; my ($pin,$value) = split ',',$_; $max_trans{$pin} = $value; } close FINAL1; #print Dumper \%max_trans; open OUTFILE,'>','File_3.txt' or die "Can not open Output File_3 : $!"; open IFILE,'<','File_1.txt' or die "Can not open Input File_1 : $!"; my $pin_name; while ( my $line4 = <IFILE>){ print OUTFILE $line4; if ($line4 =~ m/\s*pin\s*\(([^)]+)/g){ $pin_name = $1; $pin_name =~ s/"//g; print "$pin_name\n"; } if ( $line4 =~ m/^\s*direction\s*:\s*output/g){ if (exists $max_trans{$pin_name}){ print OUTFILE " max_transition : $max_trans{$pin_name};\n"; } } } close IFILE; close OUTFILE;
        poj