CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: Re: [earachefl] Regex matching question: Edit Log



earachefl
Novice

Jul 17, 2009, 7:41 AM


Views: 4293
Re: [earachefl] Regex matching question

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:

Code
#!/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)
Attachments: MuchAdoAboutNothing.txt (123 KB)
  Beatrice.txt (15.0 KB)
  Saved Terminal Output.txt (22.4 KB)


Edit Log:
Post edited by earachefl (Novice) on Jul 17, 2009, 7:44 AM
Post edited by earachefl (Novice) on Jul 17, 2009, 7:44 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives