Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   making replacements in a file (http://www.velocityreviews.com/forums/t870072-making-replacements-in-a-file.html)

Jeff Thies 07-11-2003 01:12 AM

making replacements in a file
 
I have a file that I need to make some substitutions in:

open FH, "my_file" or die "Can't open... $!";

@file=<FH>;

Can I read the file handle so it is a scalar and I can just do this?:

$file=~s/replace_me/with_this/gis; # small files

and then print back to a filehandle.

Or should I do something like this:

my @new_file=();
while(<FH>){
$_=~s/replace_me/with_this/gi;
push @new_file, $_;
}

and then print @new_file to a filehandle? Or would that be a reference
to @new_file?

Seems like there should be a better way than any of these.

Jeff

Bob Walton 07-11-2003 03:53 AM

Re: making replacements in a file
 
Jeff Thies wrote:

> I have a file that I need to make some substitutions in:
>
> open FH, "my_file" or die "Can't open... $!";
>
> @file=<FH>;
>
> Can I read the file handle so it is a scalar and I can just do this?:



Sure. Slurp it. Set the value of $/ to undef, perhaps like:

{local $/; #make $/ undef only for the duration of this block,
#so it doesn't screw up possible subsequent reads.
$file=<FH>; #slurp entire file to scalar.
}

This sets the input record separator string to undef, which is a special
case that causes file "slurping". See:

perldoc perlvar

for more info.


>
> $file=~s/replace_me/with_this/gis; # small files
>
> and then print back to a filehandle.
>
> Or should I do something like this:
>
> my @new_file=();
> while(<FH>){
> $_=~s/replace_me/with_this/gi;
> push @new_file, $_;
> }
>
> and then print @new_file to a filehandle? Or would that be a reference
> to @new_file?



That, of course, would also work, unless the "replace_me" string
contains stuff which is supposed to match over a newline. The fact that
you included the "s" regexp modifier in your previous "slurp version"
regexp indicates that perhaps you intended for that to be the case
(otherwise, "s" would serve no purpose).

Of course, you don't really have to push all the lines out to an array
in the above -- just write out the modified lines as you read them.
That gives your program the distinct advantage of being able to process
a gigabyte file on your old 386 with 16 Mb of memory. Just replace the
push line with something like:

print OUT $_;

and place something like:

open OUT,">output.file" or die "Oops, $!";

before the while loop.


>
> Seems like there should be a better way than any of these.



There's always more than one way to do it. Which is "better" depends on
a lot of stuff you haven't defined for us, as well as your opinion of
what "better" means.


>
> Jeff
>


--
Bob Walton


Mina Naguib 07-11-2003 04:57 AM

Re: making replacements in a file
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jeff Thies wrote:
> I have a file that I need to make some substitutions in:
>
> open FH, "my_file" or die "Can't open... $!";
>
> @file=<FH>;
>
> Can I read the file handle so it is a scalar and I can just do this?:
>
> $file=~s/replace_me/with_this/gis; # small files
>
> and then print back to a filehandle.
>
> Or should I do something like this:
>
> my @new_file=();
> while(<FH>){
> $_=~s/replace_me/with_this/gi;
> push @new_file, $_;
> }
>
> and then print @new_file to a filehandle? Or would that be a reference
> to @new_file?


You can let perl worry about opening the files, writing back to them, and even looping over the
lines, through command-line switches. This program does what you're doing above:

- -- cut here --
#!/usr/bin/perl -pi

s/replace_me/with_this/gi;
- -- cut here --

That's it ! For more info on the -p and the -i switches (and many other interesting ones), see
perldoc perlrun


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/DkOteS99pGMif6wRAvrYAKD/oylKq/WL6pLBixWlA9VCng542QCfY/dt
BbWHBtt+1GK7PCGVriMaMuA=
=6jHE
-----END PGP SIGNATURE-----



All times are GMT. The time now is 11:53 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57