Since you asked for comments, I'll make a few:
- main improvement is to make better indenting
- if(-d $indir) was unnecessary
- when you do a readdir, this returns only the names (not full paths) and this will include any directories (including the . and .. ones!). It is common to use a grep to filter out the stuff that you don't want.
- always check whether any kind of file operation succeeded or not
- declare variables when you actually use them the first time.
I didn't actually run this so excuse me if I made a mistake.
#!/usr/bin/perl
use strict;
use warnings;
my $indir = 'C:/input';
my $outdir ='C:/output';
opendir(DIR, $indir) or die "can't open directory $indir $!";
foreach my $file (grep{-f "$indir/$_"}readdir DIR)
{
open IN, '<', "$indir/$file" or die "can't open $indir/$file $!";
my $new= "$outdir/$file";
open OUT, '>', $new or die "can't open $new for output $!";
while (my $string = <IN>)
{
undef ($/);
while ($string =~m/(FINDINGS|COMPLICATIONS)(:)(.*?)(^[A-Z])/sgm
+)
{
print "processing $file\n";
print OUT "$1$2\t$3";
}
}
close IN;
close OUT;
}
closedir(DIR);
update: these "close" statements aren't strictly necessary, all file handles will get closed when your program exists. When you open IN for the next file, this automatically closes the current IN file (if there is one). exit() wasn't necessary, so I took it out.
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.