in reply to Re: file compare and populate
in thread file compare and populate

This code only prints the new items from file2. The main problem from Anonymous Monk's code was that it used the complete lines as hash keys...

Anonymous Monk probably needs something smarter, or more perlish, than the following snippet :
open FIRST,"file1" or die "Can't open file1: $!\n"; open LAST,"file2" or die "Can't open file2: $!\n"; my %names ; my $write_new = 0 ; while (<FIRST>) { /(\w*) (\d*)/ ; $names{$1} = $2 ; } close FIRST; while (<LAST>) { /(\w*) (\d*)/ ; if (!defined($names{$1})) { $write_new++ ; $names{$1} = $2 ; } } close LAST; if ($write_new) { open NEW,">","file3" or die "Can't open file3: $!\n"; foreach (sort keys %names) { print NEW "$_ $names{$_}\n" ; } close NEW; }

Gu

Updated to avoid unnecessary opening of new file.

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.