#!/usr/bin/perl
# w6.4.pl:  Change data in a field.
# To try this   cp ~horn/public_html/tc392/perl/w6.2.fdb temp.fdb 
# Usage:  w6.4.pl Horn "Everyday Zen"
# If Horn exists change his book to "Everyday Zen"

undef($/);			#no boundaries; data read as 1 string
open (FH, "temp.fdb");
$contents = <FH>;
close (FH);

#Make substitutions
@records = split(/^Last:/m, $contents); 
shift(@records);
foreach (@records){
   $_ = "Last:" . $_; 				#Restore Last;

   if ($_ =~ m/^Last: \s*$ARGV[0]/m){ 		#Searching for last name
      s/Book:\s*.*\s*\n/Book: $ARGV[1]\n/m;	#Make the book substitution
   }
}

#Replace database with @records (now containing substitutions).
open (OUT, ">temp.fdb");		# Now open db for write.
foreach (@records){
   print (OUT "$_");
}
close (<OUT>);
