#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;


my %hours_worked_by;

while (<DATA>) {
   my ($emp, $hour_string) = split;
   my @hours = split /,/, $hour_string;

   # $emp is the name of an employee
   # @hours is a list of hours worked by that employee this week

   # *****1*****
   # add a new key/value pair to the hash %hours_worked_by
   # where the key is the employee, and the value is
   # a reference to the array of hours worked. 
}
   
# Uncomment the following line to examine %hours_worked_by
# and verify you've built the hash correctly:
#print Dumper(\%hours_worked_by);
# Please re-comment this line before submitting.  Thank you.

while (1) {
   print "Please enter an employee name (or 'done' to end):\n";
   chomp (my $name = <STDIN>);
   last if $name eq 'done';
   exists $hours_worked_by{$name} or 
     warn "Invalid name, not in our system\n" and next;

   # *****2*****
   # Call the subroutine total_hours, passing in the employee name.
   # Store the result in $total_worked
  

   print "$name worked $total_worked hours this week\n";


   print "Enter a string of days of the week, comma-separated, each day 0-4\n";
   chomp (my $day_str = <STDIN>);
   my @days = split /,/, $day_str;
   for my $day (@days) {
      if ($day !~ /^[0-4]$/) {
         print "Invalid response, skipping\n";
         next;
      }
   }

   # *****3*****
   # Call the subroutine hours_on, passing in the employee name and the days. 
   # store the results in @worked


   for my $i (0..$#days) {
      print "$name worked $worked[$i] hours on day $days[$i]\n";
   }
}


# *****4*****
# Fill in the subroutine total_hours.  It takes an employee
# name, and returns the total number of hours that employee
# worked. 
sub total_hours {

}


# *****5*****
# Fill in the subroutine hours_on.  It takes an employee name
# and a list of the days of the week (0 - 4).  Return a list of the number of hours
# that employee worked on each of those days.
sub hours_on {

}


# *****6*****
# ***BONUS***
# Write a subroutine sort_employees.  It takes a single argument,
# either the letter 't' or a number from 0 - 4.  Print out a
# list of all employees, the hours they worked each day, and
# the total hours worked.  
# If the argument passed in is a number from 0 - 4, order
# this printed list by the number of hours worked that day. 
# (for example, if the number is 2, whoever worked the most
# hours on day 2 is printed first).  If the argument is 't',
# order the list by the total number of hours worked.
# In all cases, fall back to the employee name if the sort criteria
# is equal.
# Please format your output as follows:
# Name: H1, H2, H3, H4, H5 = Total
###################################
# sub sort_employees {
#
# }

if (defined &sort_employees) {
   print "How do you wish to sort? (0-4 or t):\n";
   chomp (my $sort = <STDIN>);
   sort_employees($sort);
}


__DATA__
Paul 8,8,8,8,8
Rob 8,4,10,10,10
Sue 5,5,5,5,5
Mark 12,6,6,6,12
Priya 0,4,0,4,4
Kevin 11,9,10.5,10,9.5
