Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.


In reply to Re: How to use 2 files for calculating charges by jwkrahn
in thread How to use 2 files for calculating charges by rruser

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (11)
    As of 2014-11-28 12:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (196 votes), past polls