Your code is incredibly long for something that looks pretty simple, if I understand it correctly.

You haven't described your files, but I assume that the three first columns, put together, define a unique identifier for the tasks you want to process.

You first need to read the first file and store in a hash the 3-day gratuity left-overs. Something like this (untested):

my %free_days_left; while (<$file1>) { chomp; my @fields = split /\s+/, $_; my $key = join "-", @fields[0..2]; $free_days_left{$key} = $fields[5]; }

This could be made more compact, but I prefer just to explain the process as clearly as possible.

Now, the %free_days_left hash contains the free days left. You just need to read through the second file, compute the number of days, subtract the free days and output the result (again, a general idea, not tested).

while (<$file2>) { chomp; my @fields = split /\s+/, $_; my $key = join "-", @fields[0..2]; my ($start_date, $end_date) = @fields[5,6]; my $start_day = (split /,/, $start_date)[2]; my $end_day = (split /,/, $end_date)[2]; my $duration = $end_day - $start_day + 1: my $corrected_duration = exists $free_days_left{$key}? $duration +- $free_days_left{$key} : $duration; print $file3 join " ", $_, $duration, $corrected_duration, $corre +cted_duration * $unit_price, "\n"; }

OK, the formating of the output file may need a bit more work, but I guess you've got the basic idea.


In reply to Re: How to use 2 files for calculating charges by Laurent_R
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!
  • 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:
    & &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.