C
ccc31807
This isn't strictly Perl question, but I've implemented this in Perl, and will re-implement it in Perl.
I have several contracts to build about five times a year. Among other provisions, the 'contract price' is one of the provisions. While some are negotiated (and therefore beyond my ken) most can be calculated. I have built a function to calculate the pay, to which I pass about nine different parameters, and it returns a discrete integer, the 'contract price.' Rather than describe it, there is some semi-pseudo code below. As you will note, it consists of a large number (almost 100) of if-elsif statements.
Question: can someone recommend a better approach?
Thanks, CC.
my %contracts = get_contract_info_from_database();
foreach my $contract (keys %contracts)
{
#initialize about 20 different variables that constitute the contract terms
my ($site, $rank, $experience, $ftptstat, $jobtype, $level ... ) = parse_row_from_hash($contracts{$contract);
$contract_price = calculate_contract_price($site, $rank, $experience, $ftptstat, $jobtype, $level ...);
#build and print each contract
}
sub calculate_contract_price
{
my ($site, $rank, $experience, $ftptstat, $jobtype, $level ... ) = @_;
my $contract_price = 0;
if ($jobtype eq 'a')
{
if ($level == 1)
{
if ($rank eq 'A')
{
# etc for the rest of the nine parameters until, at the bottom, I have this:
# $contract_price = $hours * $production * 1288;
# (1288 constitutes the approved price for the work to be performed at the
# particular level, rank, site, time status, job type, etc.
}
elsif ($rank eq 'B')
{
}
elsif ($rank eq 'C')
{
}
else { warm "ERROR in rank, $rank\n"; }
}
elsif ($level == 2)
{
}
elsif ($level == 3)
{
}
else { warn "ERROR in level: $level\n"; }
}
elsif ($jobtype eq 'b')
{
}
elsif ($jobtype eq 'b')
{
}
else { warn "ERROR in jobtype: $jobtype\n";
return $contract_price;
}
I have several contracts to build about five times a year. Among other provisions, the 'contract price' is one of the provisions. While some are negotiated (and therefore beyond my ken) most can be calculated. I have built a function to calculate the pay, to which I pass about nine different parameters, and it returns a discrete integer, the 'contract price.' Rather than describe it, there is some semi-pseudo code below. As you will note, it consists of a large number (almost 100) of if-elsif statements.
Question: can someone recommend a better approach?
Thanks, CC.
my %contracts = get_contract_info_from_database();
foreach my $contract (keys %contracts)
{
#initialize about 20 different variables that constitute the contract terms
my ($site, $rank, $experience, $ftptstat, $jobtype, $level ... ) = parse_row_from_hash($contracts{$contract);
$contract_price = calculate_contract_price($site, $rank, $experience, $ftptstat, $jobtype, $level ...);
#build and print each contract
}
sub calculate_contract_price
{
my ($site, $rank, $experience, $ftptstat, $jobtype, $level ... ) = @_;
my $contract_price = 0;
if ($jobtype eq 'a')
{
if ($level == 1)
{
if ($rank eq 'A')
{
# etc for the rest of the nine parameters until, at the bottom, I have this:
# $contract_price = $hours * $production * 1288;
# (1288 constitutes the approved price for the work to be performed at the
# particular level, rank, site, time status, job type, etc.
}
elsif ($rank eq 'B')
{
}
elsif ($rank eq 'C')
{
}
else { warm "ERROR in rank, $rank\n"; }
}
elsif ($level == 2)
{
}
elsif ($level == 3)
{
}
else { warn "ERROR in level: $level\n"; }
}
elsif ($jobtype eq 'b')
{
}
elsif ($jobtype eq 'b')
{
}
else { warn "ERROR in jobtype: $jobtype\n";
return $contract_price;
}