Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-26-2012, 12:27 AM   PM User | #1
bazz
Master Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 5,211
Thanks: 39
Thanked 201 Times in 197 Posts
bazz will become famous soon enoughbazz will become famous soon enough
rusty on comparing two hashes

Hi,

Been working away from the 'puter for a while and now I'm back, I have forgotten something. seem to be very foggy.

I need to compare/contrast the following two hashes. I need to know if the times of day are the same for each day of the week. if they are standard throughout the week I need to raise one specific value (eg 'standard') and if there is even just one time of day that is not related to all days, then I need to raise the value 'variable'.

My attempt is beneath these hashes but would anyone please advise firstly, if the comparison will identify any specific time that is not available on every day? (I just want to indentify if there is any variation in times across the week.

Secondly, is there a simpler way to code this? if so, can you give me a pointer

Code:
$days_and_times = {
          'Saturday' => {
                          '10:30 AM' => 3,
                          '12:15 PM' => 3,
                          '4:15 PM' => 3,
                          '2:15 PM' => 3
                        },
          'Tuesday' => {
                         '10:30 AM' => 3,
                         '12:15 PM' => 3,
                         '4:15 PM' => 3,
                         '2:15 PM' => 3
                       },
          'Sunday' => {
                        '10:30 AM' => 3,
                        '12:15 PM' => 3,
                        '4:15 PM' => 3,
                        '2:15 PM' => 3
                      },
          'Wednesday' => {
                           '10:30 AM' => 3,
                           '12:15 PM' => 3,
                           '4:15 PM' => 3,
                           '2:15 PM' => 3
                         },
          'Friday' => {
                        '10:30 AM' => 3,
                        '12:15 PM' => 3,
                        '4:15 PM' => 3,
                        '2:15 PM' => 3
                      },
          'Monday' => {
                        '10:30 AM' => 3,
                        '12:15 PM' => 3,
                        '4:15 PM' => 3,
                        '2:15 PM' => 3
                      },
          'Thursday' => {
                          '10:30 AM' => 3,
                          '12:15 PM' => 3,
                          '4:15 PM' => 3,
                          '2:15 PM' => 3
                        }
        };

$times_per_day = {
          '10:30 AM' => {
                          'Tuesday' => 3,
                          'Saturday' => 3,
                          'Sunday' => 3,
                          'Wednesday' => 3,
                          'Friday' => 3,
                          'Thursday' => 3,
                          'Monday' => 3
                        },
          '12:15 PM' => {
                          'Tuesday' => 3,
                          'Saturday' => 3,
                          'Sunday' => 3,
                          'Wednesday' => 3,
                          'Friday' => 3,
                          'Thursday' => 3,
                          'Monday' => 3
                        },
          '4:15 PM' => {
                         'Tuesday' => 3,
                         'Saturday' => 3,
                         'Sunday' => 3,
                         'Wednesday' => 3,
                         'Friday' => 3,
                         'Thursday' => 3,
                         'Monday' => 3
                       },
          '2:15 PM' => {
                         'Tuesday' => 3,
                         'Saturday' => 3,
                         'Sunday' => 3,
                         'Wednesday' => 3,
                         'Friday' => 3,
                         'Thursday' => 3,
                         'Monday' => 3
                       }
        };
Code:
foreach my $time_of_day ( keys %times_per_day ){
      
    my @days_of_week = keys %{$times_per_day{$time_of_day}};
	  
    foreach my $day ( @days_of_week ){
	  
        my @datd= (keys %{$days_and_times{$day}}); 
        if (!grep /$time_of_day/ , @datd ){ # if any $time_of_day is not shown for any day_of_week 
	    $times_type ='varied';
	}
	  
    }
	  
}
bazz
__________________
"The day you stop learning is the day you become obsolete"! - my late Dad.

Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
Useful MySQL resource
Useful MySQL link
bazz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:13 PM.


Advertisement
Log in to turn off these ads.