in reply to Replace value in the text file
Leaving aside the issue of better ways to parse a CSV file, and single letter var names. But fixing use strict/warnings via my, and fixing bareword filehandles and 2 arg opens, and fixing that changing %hash1 did nothing to what was in @a
#!/usr/bin/perl use strict; use warnings; open my $fh2,"<","file2.txt"; my %hash2; while (my $line=<$fh2>){ chomp $line; my @k = split ',', $line; $hash2{$k[0]} = $k[1]; } close $fh2; open my $fh1,"<","file1.txt"; my @final; while (my $line=<$fh1>){ chomp $line; my @k = split ',', $line; $k[1] = $hash2{$k[0]} if (exists $hash2{$k[0]}); push @final,join(',',@k); } close $fh1; open my $fh3,">","file1.txt"; for my $value (@final) {print $fh3 $value."\n";} close $fh3;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Replace value in the text file
by Anonymous Monk on Jun 28, 2017 at 06:48 UTC |