
BillKSmith
Veteran
Dec 12, 2012, 5:21 PM
Post #2 of 5
(13685 views)
|
Re: [Thalakos] Merges 2 text files under few very difficult conditions
[In reply to]
|
Can't Post
|
|
Try this as version 1. I think it does what you want.
use strict; use warnings; open my $B, '<', 'file_B.txt' or die "Cannot open file_B: $!"; my @functions; my %toc; my $i =0; while (my $line = <$B>) { chomp $line; $line =~ s/(\d+)\s//; $functions[$i] = $line; $toc{$1} = $i++; } close $B; open my $A, '<', 'file_A.txt' or die "Cannot open file_A: $!"; open my $C, '>', 'file_C.txt' or die "Cannot open file_C; $!"; my $prev_id = 'foo'; while (my $line = <$A>){ my ($id,$code) = split /\s+/, $line; if ($id ne $prev_id){ print {$C} "\n" if $prev_id ne 'foo'; print {$C} "$id "; $prev_id = $id; } else { print {$C} "\t"; } print {$C} $functions[$toc{$code}]; } print {$C} "\n"; close $C; close $A; Good Luck, Bill
|