Hello Perl experts,
I am pretty new to Perl and having a memory problem with my code. I have narrowed it down to the following section of code which always seems to generate the out of memory error when running.(when the number of files are large).
opendir (TXT, "$processedDirPath") or die "Cannot open
+ $processedDirPath directory $!\n";
while ( (my $matchingFiles = readdir(TXT)) ) {
if ( $matchingFiles =~ /\.rtsd001$/ ) {
my @fileContents;
my $UNMODIFIED;
my $MODIFIED;
open(UNMODIFIED, "<$processedDirPath/$matching
+Files") or die "Cannot open $UNMODIFIED for reading $!\n";
open(MODIFIED, ">$processedDirPath/$matchingFi
+les.old") or die "Cannot open $MODIFIED for writing $!\n";
while (<UNMODIFIED>) {
/STRUC20/ and @fileContents=(), next or pu
+sh @fileContents, $_; # All files that end in rtsd001 will need to be
+ modified.
}
print MODIFIED @fileContents;
close(UNMODIFIED);
close(MODIFIED);
system( "/bin/mv", "$processedDirPath/$matchin
+gFiles.old", "$processedDirPath/$matchingFiles" ) == 0 or warn "Move
+command filed $!\n";;
}
}
closedir TXT;
Now a little background. I want to process all files in a directory which is held by $processedDirPath variable and only work with files that end with "rtsd001".
I have also tried saving the readdir to an @array then grepping for the file extensions I want and doing a while loop against that.
What is the MOST memory efficient/best way to read files in a directory then modify each file found (Script will have to rewrite all files to remove lines before the STRUC20 line.)
The directory which I am running readdir against contains about 6-10 thousand files each about <1k - 40k> in size.
So, working with this large number of files what would be the best way to do this?
Thanks for the help in advance.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.