thanks for help, here's my solution, any further comments are welcomed... thanks, Pam
#!/usr/bin/perl -w use strict; #Purpose: replace Instructor: Name with bio from backmatter open(CAT, "catalog.txt") or die ("no IN file: $!"); open(BIOS, "instructor.txt") or die ("no instructor file: $!"); open(OUT, ">out") or die ("no out file: $!"); select (OUT); get_name(); sub get_name{ my ($match, $line); while ($line = <CAT>){ $match = '^Instructor:\s+([A-Z].+$)'; if($line =~ /$match/){ print("YES: $1\n"); print("1: $line\n"); replace_it($1); next; } else { print("NO: $line\n"); } } } #end get_name sub replace_it{ my $end = "XXX"; my ($replace, @lines); print("2: $1\n"); while ($replace = <BIOS>){ if($replace =~ $1){ push(@lines, $replace); last if($replace =~ $end) } } print("SUB: @lines\n"); return; } #end replace_it close CAT; close BIOS; close OUT; >>> "R. Joseph Newton" <[EMAIL PROTECTED]> 03/13/03 10:45 AM >>> Pam Derks wrote: > Hi all, Hi Pam, I think you may be making it harder than it needs to be. I will add some inline comments . > In FILE1 I want to replace all the lines containing: > Instructor: [A-Z].+ You are jumping far too quickly into code here. This should be in english. Are you actually concerned right now with the capitalization of the instructors first name? > with the complete biography for each instructor in FILE2 What do numbers 1 and 2 tell you about the content of the file or its or usage in the context of your task. There is no need to throw away meaning. Try NAMES and BIOS instead. > or > replac this text in FILE1 > Instructor: GENE ABBOTT > > with this text from FILE2 > GENE ABBOTT, M.A., has worked in vocational and adult education since 1973. He has > been > department chair, lead teacher, mentor, and consultant, and is program manager for > the Marin > County Regional Occupational Program. > > Problem: > 1 .I'm getting an infinite loop on the instructors' names in replace_it() > 2. and @replace never gets the instructors' bios > > any clues would be appreciated, > > thanks in advance, Pam > > ----------------------------------------------------- > code thus far > > #!/usr/bin/perl -w > use strict; > > #Purpose: replace Instructor: Name with bio from backmatter > > open(FILE1, "+<write.txt") or die ("no IN file: $!"); This is a text file, not a fixed-width data file. Therefore this is an inappropriate open mode. See David Wagners comments. A more meaningful file name would also be helpful here. This line should probably move down a little, also. > open(FILE2, "instructor.txt") or die ("no instructor file: $!"); > > my @bios = (); I am going to make an assumption here, based on the content of the course listings, that write.txt is probably something like course_catalog.txt, and that there are one or more listing for each instructor in the catalog. Given that, it is probably a good idea, as David pointed out, to load the biographies into a hash keyed to instructor name at this point. That way, you have the information close at hand while you are processing the catalog file. > my $bio; > > get_name(); > > sub get_name{ > my ($match, $line); > > while ($line = <FILE1>){ > $match = '^Instructor:\s+([A-Z].+$)'; > foreach ($line =~ /$match/){ > push(@bios, $1); > } > } You will probably want to rework this. There should be n o need to store any of the lines from the catalog file. You can simply process them, using the biography datastored in your hash, and pass them through to the temp file > replace_it([EMAIL PROTECTED]); > > } #end get_name > > sub replace_it{ You should not need this function at all now. > } #end replace_it > > > close FILE1; > close FILE2; Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]