in reply to Last value update is only happening instead of all.

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

Replies are listed 'Best First'.
Re^2: Last value update is only happening instead of all.
by anirbanphys (Beadle) on Aug 26, 2018 at 05:57 UTC

    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

        Thank you POJ for the suggestion and showing how to do in an effective way :). Yes, I will use your code, the way you have done it is excillent. I was trying to do in another way, though I am not feeling proud to say my code is a good one. But just tried to make it work. But without your help it was not possible. Thanks to both POJ and PRYRT, your suggestion works for me and atleast I tried to think other options. Thanks again :).

        my $pin_name; my $out_pin; my $outdir_pin; my $flag_pin=0; my $csvoutpin; my $max_tran_val; open (FILE2,"<","File_2") || die "Can not open Final_output.csv File"; my @csvFile = <FILE2>; open (FILE1,"<","File_1") || die "Can not open libray File"; my @libFile = <FILE1>; for (my $i = 0; $i< @libFile; $i++) { my $line = $libFile[$i]; chomp $line; if ($line =~m/^\s*pin\s*\(\"(.*)\"\)/g || $line4 =~m/^\s*pin\((. +*)\)/g) { $pin_name = $1; chomp $pin_name; } if ($line =~m/^\s*direction\s*:\s*output/g) { $out_pin = $pin_name; $outdir_pin = $line; $flag_pin =1; } if ($flag_pin == 1) { foreach my $csvoutpinLine (@csvFile) { chomp $csvoutpinLine; my @tempLine = split(',',$csvoutpinLine); my $csvoutpin=$tempLine[0]; my $max_tran_val=$tempLine[1]; if ($csvoutpin eq $out_pin) { $line =~ s/$outdir_pin/$outdir_pin\n max_transiti +on :$max_tran_val /g ; $libFile[$i] = $line; } } $flag_pin=0; } } open (FILE3,">","File_3") || die "Can not open libray File"; print FILE3 "@libFile" ; close FILE1; close FILE2; close FILE3;