Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-24 17:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found