http://www.perlmonks.org?node_id=1049379


in reply to How to use 2 files for calculating charges

my $cnt = 0; ... $cnt++; ... print $cnt, "\n";

You don't really need the $cnt variable.    Just remove the first two lines and change the last line to:

print "$.\n";


my $filename = '$mainDir\test.txt';

Variables don't interpolate in single quoted strings so you should change that to a double quoted string:

my $filename = "$mainDir\test.txt";


my $mth_end = sprintf "%04d,%02d,%02d",$y,$m,Days_in_Month($y,$m);

You never use $mth_end anywhere.



printf OUT " Placement Release + Tot Chg\n"; printf OUT " Date Date + Days Days Cost\n";

You should be using print instead of printf:

print OUT " Placement Release + Tot Chg\n"; print OUT " Date Date + Days Days Cost\n";


my @dates = split '\s+', $_;

Or just:

my @dates = split;


if ($free >= 0) { my $amt = $free; my $cost = ($amt * 100); my $pfmt = "%-14s %-5s %-8s %-3s %-12s %-12s %-6s %-6s %-6s\n"; printf OUT $pfmt, $dates[0], $dates[1], $dates[2], $dates[3], fmt +_mdy($dates[4]), fmt_mdy($dates[5]), $diff, $amt, fmt_curr($cost); } elsif ($free < 0) { my $amt = 0; my $cost = ($amt * 100); my $pfmt = "%-14s %-5s %-8s %-3s %-12s %-12s %-6s %-6s %-6s\n"; printf OUT $pfmt, $dates[0], $dates[1], $dates[2], $dates[3], fmt_ +mdy($dates[4]), fmt_mdy($dates[5]), $diff, $amt, fmt_curr($cost); }

You have a lot of code duplication in there.    You could simplify that as:

my $amt = $free < 0 ? 0 : $free; my $cost = $amt * 100; my $pfmt = "%-14s %-5s %-8s %-3s %-12s %-12s %-6s %-6s %-6s\n"; printf OUT $pfmt, @dates[ 0, 1, 2, 3 ], fmt_mdy( $dates[ 4 ] ), fmt_ +mdy( $dates[ 5 ] ), $diff, $amt, fmt_curr( $cost );


$ymd =~ s/ //g; # return ' 'x10 if $ymd eq $NULL_DATE; my ($y,$m,$d) = split /\D/,$ymd;

Why remove spaces when they match /\D/ which your split will remove anyways?    The problem is that split /\D/,$ymd will only remove single non-digit characters and you need to remove multiple non-digit characters:

my ( $y, $m, $d ) = split /\D+/, $ymd;

Or just capture the digit characters:

my ( $y, $m, $d ) = $ymd =~ /\d+/g;


while (<$file>) { ... sub fmt_curr { ... sub fmt_mdy { ... }

Subroutine names are available globally so putting them inside a while loop does not effect their scope, but it might complicate the use of variables inside the subroutine.