Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

trouble shooting my priority lists

by arcnon (Monk)
on Jun 15, 2007 at 00:59 UTC ( [id://621377]=perlquestion: print w/replies, xml ) Need Help??

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

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:

Replies are listed 'Best First'.
Re: trouble shooting my priority lists
by cdarke (Prior) on Jun 15, 2007 at 10:58 UTC
    I may have missed something obvious, and excuse me for not reading every line of code, but it seems to me that the output is correct. In your 'list_' subroutines you are printing the same hash each time.
    For example, in list_holidays you are sorting %p_list_holidays but printing %p_list_weekdays.
    That could be what you intend, if so then I have misunderstood the problem, and apologies.
      ARRRG!!!! just kill me now. 6 hours. Yep that is what is wrong thanks works as it should.
Re: trouble shooting my priority lists
by naikonta (Curate) on Jun 15, 2007 at 03:03 UTC
    it appears that when I sort my hashes it is replicating into all my hashes. I am clueless about what I am doing wrong.
    It's pretty hard to find out what could be wrong in such lengthy code, without you explaining what do you expect, what the code output that you think incorrect, and a set of data sample. One suggestion is to use Data::Dumper to inspect your data structure. And if you already have spotted a portion of your code that smells, it would help the rest of us to help you if you just posted that portion instead of the full script.

    Please don't use the Switch module for production, per its own author's advice. Many people have experienced problems using it. Here are some reasons why. But you can use some simpler alternative. I see, there are only two possible branches so you may acutally only need a boolean test.

    my $target_days; if ($data{day_name} =~ /fri/i) { $target_days = \%p_list_fridays; $fridays++; } else { $target_days = \%p_list_weekdays; $weekday++; } $target_days->{$data{doc}}++; # instead of # $target_days->{$data{doc}} = $target_days->{$data{doc}}+1;
    I'm sure this can be improved, but it's just a quick answer.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      I did say what I was expecting & what is wrong. Dont need Dumper because I seperated out my hashs to ease trying to debug this. Like I said I think that my 4 sorting routines are messing the hashes up but I dont know how.

      As for pulling a smaller code chunk out.... You cant you need it all to have some sort of context to the issue. Hence I will to explain more concisely what the issue is. Saying
      $p_list_weekends{$a} <=> $p_list_weekends{$b};
      $p_list_weekdays{$a} <=> $p_list_weekdays{$b};
      ect. Is replicationg into all my hashes means nothing without seeing it in context.

      To expound on my explaination:
      %p_list_holidays: should return nothing but names => 0
      %p_list_fridays: all the values added together should be 52
      %p_list_weekends: all the values added together should be 104
      %p_list_weekdays: all the values added together should be 209

      Look at the following it is easy to see that
      1 they are all the same
      2 are not following the above math
      3 the hash %p_list_holidays is NOT accessed once but is populated with the same data as the rest.
      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
Re: trouble shooting my priority lists
by varian (Chaplain) on Jun 15, 2007 at 06:44 UTC
    For sure the sorts don't mess around with your hashes, they merely peek into it and produce a sorted list of keys.

    However the sub check_around is probably not doing what you intended it to do. It will always return the same value as what $doc brings. The variable @doc_block is not declared and therefore its values are undef.
    Update:$doc_block[1] is always undef (you shift the array if the number of elements>1 elsewhere in your code).

    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; } }

    NB: use warnings would have signalled the issue and it would have saved you time.

      yes good catch. the array should push & shift the current doctor that I use thur each itenoration so as they dont work twice in any 5 day streach. That was another trouble spot that I thought I had cut away.
      As you can tell I cut this fish to the bone abeit very sloppily.
Re: trouble shooting my priority lists
by Errto (Vicar) on Jun 16, 2007 at 01:07 UTC
    Your priority lists seem pretty important. Please don't shoot them.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://621377]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found