in reply to Get data from a file to search and replace within a second file
One functional flaw with your solution is that you keep overwriting your output file every time you open it for output. Thus, you lose the results of your previous substitution.use strict; use warnings; my $fhi; my %data; open $fhi, '<', 'file_A.txt' or die "can not open file file_A.txt: $!" +; while (<$fhi>) { chomp; my @cols = split /\t/; $data{$cols[2]} = "@cols[2..3]"; } close $fhi; open $fhi, '<', 'file_B.txt' or die "can not open file file_B.t +xt: $!"; open my $fho, '>', 'file_B_out.txt' or die "can not open file file_B_o +ut.txt: $!"; while (<$fhi>) { for my $k (keys %data) { s/$k/$data{$k}/g; } print $fho $_; } close $fho;
Update: I like almut's $search string better than my for loop.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Get data from a file to search and replace within a second file
by biscardi (Initiate) on Mar 23, 2010 at 15:48 UTC | |
by toolic (Bishop) on Mar 23, 2010 at 16:41 UTC | |
by educated_foo (Vicar) on Mar 24, 2010 at 14:21 UTC |