PerlIsGood
Programmer
Been a while since I worked with Perl. Can someone help me out with this. I'm reading a directory of comma delimited files. I need to read in all files, add a few variables based on each file's name, and write out another file containing all files' info.
Here's what I have so far, but can't get the regex working properly...
The regular expression I'm attempting to build needs to capture this type of filename:
^(everything) - (everything) - (####.##.##)
Also -- each file has it's own header, how do i read each file starting from the second line?
ALl suggestions welcome. Thanks!
Notorious P.I.G.
Here's what I have so far, but can't get the regex working properly...
The regular expression I'm attempting to build needs to capture this type of filename:
^(everything) - (everything) - (####.##.##)
Also -- each file has it's own header, how do i read each file starting from the second line?
ALl suggestions welcome. Thanks!
Code:
$dir = "E:\\dir\\";
opendir DIR, $dir or die "Could not open $dir directory: $!";
@totalfiles = readdir DIR;
closedir DIR;
# header row
$outfile = 'item,region,date,other headers...\n';
#data
foreach ( sort @totalfiles ) {
next if ($_ =~ m/^\./);
m/^(.+?) - (.+?) - (\d4.\d2.\d2) \d+?.txt$/;
my($item) = $1;
my($region) = $2;
my($date) = $3;
open FILE, "$dir\\$_" or die "Could not open file $_: $!";
while ( chomp (@file = <FILE>) ) {
$outfile += $item . "," . $region . "," . $date . "," . shift @file;
}
close FILE;
}
$f = "E:\\dir\\file.txt";
open(OUTP, ">$f");
print OUTP "$outfile";
close(OUTP);
Notorious P.I.G.