in reply to Re^2: Replace data in the column of one file with corresponding ones in another file
in thread Replace data in the column of one file with corresponding ones in another file

The version not relying on File::Slurp would be the following:
use Tie::File; use strict; my $file1 = 'file1.txt'; my $file2 = 'file2.txt'; open my $fh, $file2 or die "Can't open $file2: $!"; my %hash = map { chomp; my @data = split /\s+/; $data[0] => $data[2]; } <$fh>; tie my @array, 'Tie::File', $file1 or die "Can't open $file1: $!"; foreach (@array) { s{^(\S*\s+)(\S+)}{ if (exists $hash{$2}) { print "Replacing $2 with $hash{$2}\n"; $1 . $hash{$2}; } else { print "Can't find $2, not changing\n"; $1 . $2; } }e; }
- Miller
  • Comment on Re^3: Replace data in the column of one file with corresponding ones in another file
  • Download Code