in reply to How can I make a regex apply to each line of a file?

Use grep map. Update: Of course, ChemBoy is entirely right about this. That's what I get for posting after a 13-hour night shift...

If you just want to print the (modified) contents of the file, you can do

open (FILE,"<$filename") or die "Cannot open: $!"; map{s/stuff/other_stuff/g;print}(<FILE>);
Or if you want to keep the modified file in an array, try
open (FILE,"<$filename") or die "Cannot open: $!"; @ary = map{s/stuff/other_stuff/g}(<FILE>);
Update: You could also do this as a one-liner:
perl -pe 's/stuff/other_stuff/g' filename.txt
Hope this helps,

JJ

Replies are listed 'Best First'.
Re:^2 Oooh, Oooh, Help Me Help Me (:)
by ChemBoy (Priest) on Oct 28, 2001 at 09:11 UTC

    No, please don't use grep that way, unless you actually want to strip lines out of your input if they don't match the substitution pattern. Use map, instead--that's what it's for.

    @footerfile = map {s/something/else/} <FILE>;

    Note that the first solution proposed using grep wouldn't have the undesired side-effects, but it's still bad coding practice--never use grep or map in void context (obfuscatory and inefficient).

    And Kage, please find a better descriptive title for your question next time...

    Update: or, of course, you could alter the array in place after reading it in, using

    s/nothing/something/ for @footerfile;

    Update 2: of course, it would help if I told you the right syntax for map, too...

    @footerfile = map {s/something/else/;$_} <FILE>;
    (thanks, Zaxo! And I wish I had as good an excuse as jj808 for screwing this one up....)



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders