#! /usr/bin/perl

# Program to substitute author codes (PREdddd) for author names in files.
# The syntax of the author_code file is <code>\t<Name>.
# The call is code2name.pl codefile bibfile outfile

# (1) quit unless we have the correct number of command-line args
$num_args = $#ARGV + 1;
if ($num_args != 3) {
  print "\nUsage: name.pl codefile bibfile outfile\n";
  exit;
}

open(CODEF,"$ARGV[0]") or die "Can't open code table file: $!\n";

$n = 0;  # Read table and put into hash
while($line = <CODEF>)
  { ($code, $name) = split("\t",$line);
	chop($name);	
	$ncd{$code} = $name;
	$n++;	
  }

print "Table read: $n entries\n\n";
close(CODEF);

# Read text and substitute. Assumes only one substitution per line.
# Change prefix as fitted

open(BIBF,"$ARGV[1]") or die "Can't open bib file: $!\n";
open(OUTFILE,">$ARGV[2]") or die "Can't open output file: $!\n";

while($line = <BIBF>)
  { $line =~ s/(SEA\d{4})/$ncd{$1}/g;  
	print OUTFILE "$line";	
}

close(BIBF);
close(OUTFILE);

