Yoyoyo Yoyoyoyo wrote:
I have a file that I need to use the substitute operator on to get rid of spaces, and apostrophes and such. The only way I can think of doing it is this:

1. Open the file and go through it one line at a time with the diamond operator. 2. Make the substitutions on the $_ variable and save the changes in an array.
3.  Close the file, then re-open it and overwrite it with the array.

I was just wondering if there was an easier or better way of doing this.

Why not slurp the file into a scalar? And you can open it for editing rather than opening and closing it twice.

    open my $file, '+<', 'myfile.txt' or die $!;
    my $content = do { local $/; <$file> };
    $content =~ s/[^\w\n]//g;
    seek $file, 0, 0;
    truncate $file, 0;
    print $file $content;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to