This isn't fancy, but it will work,

for (@files) { open OLDDATA, "< $_" or warn 'File does not open: ', $! and next; open NEWDATA, "> $_!" or warn 'File not open: ', $! and next; while (<OLDDATA>) { s/aaaaa/FFFFFFF/gi; s/bbbbb/EEEEEEE/gi; s/cccccc/GGGGGGG/gi; print NEWDATA $_; } close NEWDATA or warn $! and next; close OLDDATA or warn $! and next; rename "$_!", $_; }
This version is not much different from yours. It reads and processes the lines one at a time, instead of slurping to and array of lines. That will save memory by taking a smaller amount and reusing it. Efficient use of memory often increases speed in perl. Doing it that way requires a second file handle to write the lines to, so I opened a temporary file called "$filename!" to hold them. Once a file is done, rename does the file replacement very efficiently.

Beware that "$filename!" doesn't already exist. You probably won't have to worry about that if these files have some systemmatic naming, but if it's a problem the -e file test will help.

Update: The DATA filehandle is special to inlined data in perl. I changed the name.

After Compline,
Zaxo


In reply to Re: Changing data in alot of files by Zaxo
in thread Changing data in alot of files by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.