problem
it appears that when I sort my hashes it is replicating into all my hashes. I am clueless about what I am doing wrong.
code purpose
generate equal working shifts mon-thur,fri,sat-sun tracking by personelle in a fiscal year..
I use 4 hashs to track the total time worked. Each are keyed with person => # of days worked. I sort the hashes to get the 1st person who has worked the least in a specific hash to put them in that days shift. Included source in total and its current ouput upon execution.
any leads would be appreciated.
code t.pl
#!/usr/bin/perl
use strict;
use Data;
my @docs = qw(moe larry curly curly_joe shimp jill jack);
my $obj = Data->new(@docs);
#exit;
$obj->list_weekdays;
$obj->list_weekends;
$obj->list_fridays;
$obj->list_holidays;
$obj->day_totals;
Data.pm
package Data;
use strict;
use Date::Calc qw(:all);
use Switch;
my %p_list_weekdays;
my %p_list_weekends;
my %p_list_fridays;
my %p_list_holidays;
my @doc_block;
my $weekdays;
my $weekends;
my $fridays;
my $holidays;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {
_docs => \@_,
_days => undef,
#_a => undef,
#_b => undef,
#_c => undef,
#_d => undef,
#_e => undef,
};
bless ($self, $class);
$self->init;
return $self;
}
sub init{
my $self = shift;
$self->populate_p_lists();
$self->populate_days();
}
sub populate_p_lists{
my $self = shift;
foreach my $doc (@{$self->{_docs}}) {
$p_list_weekdays{$doc} = 0;
$p_list_weekends{$doc} = 0;
$p_list_fridays{$doc} = 0;
$p_list_holidays{$doc} = 0;
#print qq(.....$p_list_weekdays{$doc}\n);
}
}
sub populate_days{
my $self = shift;
my %weekday_name_to_number = (
sun => 0, mon => 1,tue => 2,wed => 3,thu => 4,fri => 5,sat => 6,
);
my %weekday_number_to_name = reverse %weekday_name_to_number;
my $dow = Day_of_Week(2007,11,1);
#print $dow;
my $cnt;
for(0...364){
my %data;
$data{day_name} = $weekday_number_to_name{"$dow"};
#print qq(>>$data{day_name}\n);
$self->{what_day} = $_;
switch ($data{day_name}){
case 'sun'{
$data{doc} = $self->p_list_weekends;
$p_list_weekends{$data{doc}}=$p_list_weekends{$data{doc}}
++1;
$weekends++;
}
case 'sat'{
$data{doc} = $self->p_list_weekends;
$p_list_weekends{$data{doc}}=$p_list_weekends{$data{doc}}
++1;
$weekends++;
}
case 'mon'{
$data{doc} = $self->p_list_weekdays;
$p_list_weekdays{$data{doc}}=$p_list_weekdays{$data{doc}}
++1;
$weekdays++;
}
case 'tue'{
$data{doc} = $self->p_list_weekdays;
$p_list_weekdays{$data{doc}}=$p_list_weekdays{$data{doc}}
++1;
$weekdays++;
}
case 'wed'{
$data{doc} = $self->p_list_weekdays;
$p_list_weekdays{$data{doc}}=$p_list_weekdays{$data{doc}}
++1;
$weekdays++;
}
case 'thu'{
$data{doc} = $self->p_list_weekdays;
#print qq(xxxxxx.$data{doc}\n);
$p_list_weekdays{$data{doc}}=$p_list_weekdays{$data{doc}}
++1;
#print qq($data{doc} ....$p_list_weekdays{$data{doc}}\n);
$weekdays++;
}
case 'fri'{
$data{doc} = $self->p_list_fridays;
$p_list_fridays{$data{doc}}=$p_list_fridays{$data{doc}}+1
+;
$fridays++;
}
}
#print qq($data{doc}\n);
$dow++;
if ($dow == 7){$dow = 0;}
push(@doc_block, $data{doc});
if (@doc_block > 1){
shift @doc_block;
}
#push(@{$self->{_days}}, \%data);
#print qq($cnt...$data{day_name}\n);
$cnt++;
}
}
sub sort_weekends {
#my $self = shift;
$p_list_weekends{$a} <=> $p_list_weekends{$b};
}
sub sort_weekdays {
#my $self = shift;
#$grades{$b} <=> $grades{$a};
$p_list_weekdays{$a} <=> $p_list_weekdays{$b};
}
sub sort_fridays {
#my $self = shift;
$p_list_fridays{$a} <=> $p_list_fridays{$b};
}
sub sort_holidays {
#my $self = shift;
$p_list_holidays{$a} <=> $p_list_holidays{$b};
}
sub p_list_weekends {
my $self = shift;
my $doc = undef;
foreach my $key (sort sort_weekends ( keys(%p_list_weekends) )) {
if (!$doc && check_around($key,'p_list_weekends')){
#print qq(set...$key\n);
$doc = $key;
}
}
return $doc;
}
sub p_list_weekdays {
my $self = shift;
my $doc = undef;
#foreach $key (sort hashValueAscendingNum (keys(%grades))) {
foreach my $key (sort sort_weekdays ( keys(%p_list_weekdays) )) {
if (!$doc && check_around($key,'p_list_weekdays')){
#print qq(set...$key\n);
$doc = $key;
}
}
return $doc;
}
sub p_list_fridays {
my $self = shift;
my $doc = undef;
foreach my $key (sort sort_fridays ( keys(%p_list_fridays) )) {
if (!$doc && check_around($key,'p_list_fridays')){
#print qq(set...$key\n);
$doc = $key;
}
}
return $doc;
}
sub p_list_holidays {
my $self = shift;
my $doc = undef;
foreach my $key (sort sort_holidays ( keys(%p_list_holidays) )) {
if (!$doc && check_around($key,'p_list_holidays')){
#print qq(set...$key\n);
$doc = $key;
}
}
return $doc;
}
sub check_around{
#my $self = shift;
my ($doc,$switcher) = @_;
if ($doc_block[0] eq $doc || $doc_block[1] eq $doc){
return undef;
}
else {
return $doc;
}
}
sub list_weekdays{
my $self = shift;
print qq(======================================================\n);
print qq(WEEKDAYS\n);
print qq(======================================================\n);
foreach my $key (sort sort_weekdays ( keys(%p_list_weekdays) )) {
print "\t>>$p_list_weekdays{$key}: $key\n";
}
}
sub list_weekends{
my $self = shift;
print qq(======================================================\n);
print qq(WEEKENDS\n);
print qq(======================================================\n);
foreach my $key (sort sort_weekends ( keys(%p_list_weekends) )) {
print "\t>>$p_list_weekdays{$key}: $key\n";
}
}
sub list_fridays{
my $self = shift;
print qq(======================================================\n);
print qq(FRIDAYS\n);
print qq(======================================================\n);
foreach my $key (sort sort_fridays ( keys(%p_list_fridays) )) {
print "\t>>$p_list_weekdays{$key}: $key\n";
}
}
sub list_holidays{
my $self = shift;
print qq(======================================================\n);
print qq(HOLIDAYS\n);
print qq(======================================================\n);
foreach my $key (sort sort_holidays ( keys(%p_list_holidays) )) {
print "\t>>$p_list_weekdays{$key}: $key\n";
}
}
sub day_totals{
my $self = shift;
print qq(
weekdays:$weekdays
weekends: $weekends
fridays: $fridays
holidays: $holidays\n);
}
1;
OUTPUT
======================================================
WEEKDAYS
======================================================
>>29: curly_joe
>>30: jack
>>30: jill
>>30: larry
>>30: shimp
>>30: curly
>>30: moe
======================================================
WEEKENDS
======================================================
>>29: curly_joe
>>30: jack
>>30: jill
>>30: larry
>>30: shimp
>>30: curly
>>30: moe
======================================================
FRIDAYS
======================================================
>>30: shimp
>>30: curly
>>30: moe
>>29: curly_joe
>>30: jack
>>30: jill
>>30: larry
======================================================
HOLIDAYS
======================================================
>>30: jack
>>30: jill
>>30: larry
>>30: shimp
>>30: curly
>>30: moe
>>29: curly_joe
weekdays:209
weekends: 104
fridays: 52
holidays: