----- Original Message ----- From: ""Johnson, Reginald (GTS)"" <[email protected]>
Newsgroups: perl.beginners
To: <[email protected]>
Sent: Sunday, January 17, 2010 2:42 PM
Subject: hash of Arrays


I am trying to place the date of each day of the week for each month
that has 5 weeks into a hash. For instance my hash key 'Monday' would
point to an array that has all the dates where Monday is in the fifth
week of the month.
I've got that part going. My problem is I am having a problem accessing
the hash. Data dumper shows me the data is there, but when I try to
access I am doing something wrong.




Hello Reginald

If you make 2 changes, you should be able to view your dates.

$fifthday_hash{$weekday} = \day_in_month($weekday,$mon,$year);
                                            ^

That function is returning a ref and then you take a ref of a ref (in your code).
Just remove the backslash (in front of the function call).

print "this is key =>$key<= $key $value\n";


$value here is an array ref. To print the values dereference it (with the @ sigil)..

print "this is key =>$key<= $key @$value\n";
                                                   ^

You might understand  what's going on better if you read the docs
on references and compound data structures.

- perldoc perlreftut (very short tutorial about references)
- perldoc perldsc (Perl Data Structures Cookbook)

I haven't yet examined your logic for selecting the days you want.
Do you want all dates in the fifth week of a month, by day of week?

Some months have 6 weeks. Do you want to exclude those days?

I wrote a program that collects all days of the week  that
occur 5 times in a month. The logic is simple. For any day of week, a date >= 29th of the month will fall 5 times in a month. February has to be treated differently.


Chris




# cat fifthday
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper qw(Dumper);
use Time::Local;

       my @day_Array;
       @day_Array=qw{Monday Tuesday Wednesday Thursday Friday Saturday
Sunday};
       my $weekday;
       my %fifthday_hash;
       my ($sec,$min,$hour,$mday,$mon,$year,$wday,
               $yday,$isdst)=localtime(time);
       my $today = sprintf ("%02d%02d%4d",$mon+1,$mday,$year+1900);
       $mon = $mon+1;
       $year +=1900;

       sub day_in_a_month; #predeclare
       sub day_in_a_month_driver; #predeclare
       foreach $weekday (@day_Array){
               print "this is weeekday $weekday\n";
               $fifthday_hash{$weekday} =
\day_in_month($weekday,$mon,$year);
       }

 print Dumper( \%fifthday_hash);
              while ( my ($key,$value)= each %{fifthday_hash} ) {
                       if ($key =~/Monday/) {
                       print "this is key =>$key<= $key $value\n";

                       }
              }


{snip] rest of his code and disclaimer


--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to