Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: How to use 2 files for calculating charges

by Laurent_R (Vicar)
on Aug 13, 2013 at 22:37 UTC ( #1049354=note: print w/ replies, xml ) Need Help??

in reply to How to use 2 files for calculating charges

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.

Comment on Re: How to use 2 files for calculating charges
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1049354]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-07-26 15:33 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (178 votes), past polls