
earachefl
Novice
Jul 17, 2009, 7:41 AM
Post #6 of 7
(9166 views)
|
Re: [earachefl] Regex matching question
[In reply to]
|
Can't Post
|
|
Ah, not so fast... this works until, unexpectedly, at line 249 of the text, Don Pedro's character and lines get written to the file, and at line 977, Don John's, and at 1173, 1181, 1198, 1224, 1240, 1248, 1257, 1274, 2048, Don Pedro's, and at 3101, Don John's, and at 3121, Friar Francis's. So out of some 756 character changes, 13 are incorrectly processed. I'm attaching the txt file that's being processed (MuchAdoAboutNothing.txt) as well as my outputs - Beatrice.txt and my Terminal output. And here's the complete code that I'm using:
#!/usr/bin/perl $file = "MuchAdoAboutNothing.txt"; if (-e $file && -r $file) { open (IN, "<$file") || die "Couldn't open $file, $!"; } while ($line = <IN>) { #if $line consists of only uppercase characters, set $character to $line #unless ($line =~ m/[^A-Z\s]/) what the book suggested - not so good if ($line =~ m/^[A-Z]+\s?[A-Z]*$/) { $character = $line; #used for debugging character changes print ("Character changed to $character"); } #if BEATRICE is the current character, add her lines to the lines array if ($character =~ m/BEATRICE/) { push (@lines, "$line"); } } close(IN); open(OUT, ">Beatrice.txt") || die ("Couldn't open Beatrice.txt: $!"); print OUT @lines; close(OUT); #There's also an issue with what happens when it's still Beatrice's character and there's a scene change or stage direction; those lines also get written to the file as if they were part of Beatrice's lines. But that's a separate issue and I'll work on it myself.
(This post was edited by earachefl on Jul 17, 2009, 7:44 AM)
|