T
Tom Ewall
The following code looks for files in a directory with lables of "Run
A" "Run B" "Run C" or "Run D". Each of the files has a total line.
It grabs the total, something like "$211,153" and strips out the "$"
and the ",".
The program works, but I'm sure there are more efficient/perl
idiomatic ways of doing things than how I've done them here. I'm
looking for suggested improvements.
-----------------------------------------------------------------------
#!\Perl\bin\perl
use strict;
my $total;
my @total;
my $runA;
my $runB;
my $runC;
my $runD;
my @list;
my $lmoDir = "/__lmo";
opendir(LMODIR, $lmoDir) or die "could not open LMO directory";
my @allfiles = grep !/^\.\.?\z/, readdir LMODIR; # exclude . and ..
files
closedir(LMODIR);
foreach my $file (@allfiles){
open LMO, "$lmoDir/$file" or die "Cannot open file: $!";
my @list=<LMO>;
foreach (@list) {
if (/Run/){
$run = "$_";
chomp ($run);
}
if (/Total/){
$total = "$_";
chomp ($total);
}
}
@total = split(/\t/, $total);
$total = @total[$#total - 2];
if ($run eq 'Run A'){
$runA = $total;
}
if ($run eq 'Run B'){
$runB = $total;
}
if ($run eq 'Run C'){
$runC = $total;
}
if ($run eq 'Run D'){
$runD = $total;
}
}
$_ = $runA;
s/,//;
s/\$//;
$runA = $_;
$_ = $runB;
s/,//;
s/\$//;
$runB = $_;
$_ = $runC;
s/,//;
s/\$//;
$runC = $_;
$_ = $runD;
s/,//;
s/\$//;
$runD = $_;
A" "Run B" "Run C" or "Run D". Each of the files has a total line.
It grabs the total, something like "$211,153" and strips out the "$"
and the ",".
The program works, but I'm sure there are more efficient/perl
idiomatic ways of doing things than how I've done them here. I'm
looking for suggested improvements.
-----------------------------------------------------------------------
#!\Perl\bin\perl
use strict;
my $total;
my @total;
my $runA;
my $runB;
my $runC;
my $runD;
my @list;
my $lmoDir = "/__lmo";
opendir(LMODIR, $lmoDir) or die "could not open LMO directory";
my @allfiles = grep !/^\.\.?\z/, readdir LMODIR; # exclude . and ..
files
closedir(LMODIR);
foreach my $file (@allfiles){
open LMO, "$lmoDir/$file" or die "Cannot open file: $!";
my @list=<LMO>;
foreach (@list) {
if (/Run/){
$run = "$_";
chomp ($run);
}
if (/Total/){
$total = "$_";
chomp ($total);
}
}
@total = split(/\t/, $total);
$total = @total[$#total - 2];
if ($run eq 'Run A'){
$runA = $total;
}
if ($run eq 'Run B'){
$runB = $total;
}
if ($run eq 'Run C'){
$runC = $total;
}
if ($run eq 'Run D'){
$runD = $total;
}
}
$_ = $runA;
s/,//;
s/\$//;
$runA = $_;
$_ = $runB;
s/,//;
s/\$//;
$runB = $_;
$_ = $runC;
s/,//;
s/\$//;
$runC = $_;
$_ = $runD;
s/,//;
s/\$//;
$runD = $_;