luckysing has asked for the wisdom of the Perl Monks concerning the following question:

I recently came across this question.I wanted to ask how do i solve this.I am new to references. Write a function, compute_bonuses() to calculate employee bonuses. Employee sales data are stored like this:
$emp_data = { "bob" => { salary => 38.7, # in k$/yr sales => 23, # uni +ts }, "jane" => { salary => 50.3, sales => 36, }, ... };
Employees get a 20% bonus on their base salary if their sales exceed 30 units. Your compute_bonuses() function should take a single argument like $emp_data above, and return a hashref with names as keys and bonus amounts as values. The output hashref only contains employees receiving a bonus, e.g. { "jane" => 10.06, ... }

Replies are listed 'Best First'.
Re: hash refrences
by toolic (Bishop) on Jan 18, 2011 at 00:58 UTC
    use strict; use warnings; use Data::Dumper; my $emp_data = { "bob" => { salary => 38.7, # in k$/yr sales => 23, # units }, "jane" => { salary => 50.3, sales => 36, } }; my $bref = comp($emp_data); print Dumper($bref); sub comp { my $data = shift; my %bonus; for my $e (keys %{ $data }) { if ($data->{$e}{sales} > 30) { my $b = 0.20 * $data->{$e}{salary}; $bonus{$e} = $b; } } return \%bonus; } __END__ $VAR1 = { 'jane' => '10.06' };
      Why provide complete code for what is clearly homework? The links should be enough.
        FYI this isn't homework. It's an interview question.
      Thanks for the help!!!
Re: hash refrences
by belden (Friar) on Feb 04, 2011 at 19:40 UTC
    Here's another way to approach the problem.
    sub compute_bonuses { return +{ map { $_[0]->{$_}{sales} > 30 ? ($_ => sprintf '%.02f', (100*$_[0]->{$_}{salary}/500)) : () } keys %{$_[0]}, }; }