On 6/19/01 8:30 AM, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:

> From: Brandon Barker <[EMAIL PROTECTED]>
> Date: Tue, 19 Jun 2001 11:33:27 -0400
> To: [EMAIL PROTECTED]
> Subject: macperl deletes lines ....
> 
> I'm having trouble writing a script that will remove the regular
> expression "<B>" in a text file in macperl.  At first I thought I was
> making a programming mistake but I tried the same script in Linux and it
> worked as expected; in macperl the <B> will be removed but every line
> after the first will be deleted in the output file.  I've included the
> code as well as the droplet and a sample input file "funerals" in this
> email.
> 
> #!/usr/bin/perl -w
> {
>   foreach $inFile (@ARGV) {
>       open (INNEWSFILE, $inFile);
>       open (OUTTEXTFILE,">".$inFile.".txt");
> 
>       $newsFile = <INNEWSFILE>;
>       $newsFile =~ s/<B>//g;
>       print OUTTEXTFILE $newsFile;
>      
>       close (INNEWSFILE);
>   }
> }

The code to read the entire text file will only read in the file up to and
including the first line break:

$newsFile = <INNEWSFILE>;

Since subsequent lines are not read, you are losing them in the output file.

To read in the entire file, you would have to undefine the input record
separator ($/).  This will allow the entire file to be read into $newsFile.
Otherwise, only the first line in the file will be processed.

Example:

{
    local $/;
    foreach $inFile (@ARGV) {
        open (INNEWSFILE, $inFile);
        open (OUTTEXTFILE,">".$inFile.".txt");

        $newsFile = <INNEWSFILE>;
        $newsFile =~ s/<B>//g;
        print OUTTEXTFILE $newsFile;
        
        close (INNEWSFILE);
    }
}

I could not get your script (as originally coded) to run successfully under
Linux or MacPerl.  I was surprised to hear it worked for you under Linux.

By the way, there is an article in the most recent edition of MacWorld on
Perl and OS X.  The sample script in the article did exactly the same thing
(not taking the input record separator into account, among other things).

Hope this information is helpful!
Regards,
Will


Reply via email to