#!/usr/bin/perl -w --
use strict;

my @buf;
my $i;
my %newbuf;

if(3!=scalar(@ARGV)){
	print "Usage: perl $0 datafile newdatafile replacefile";
	exit 1;
}#if

open(DATAFILE,"<$ARGV[0]");
open(NEWDATA,"<$ARGV[1]");


while($i = <DATAFILE>){ #read old data into hash
	@buf = split (/ /,$i); #split each line into space-delimited fields
	$newbuf{$buf[0]}=$buf[1]; #assign to hash, using first field as key
}#while

while($i = <NEWDATA>){ #read new data in, replacing old
	@buf = split (/ /,$i); #split each line into space-delimited fields
	$newbuf{$buf[0]}=$buf[1]; #assign to hash, using first field as key
}#while

close(DATAFILE);
close(NEWDATA);

open(REPLACE,">$ARGV[2]");

for $i ( sort keys %newbuf ) { #sort by keys and print to new file
	print REPLACE "$i $newbuf{$i}\n";
}#for

close(REPLACE);
