Hi Onteria, On Monday 13 December 2010 02:52:42 Onteria wrote: > I just finished working on a solution for the exercises provided in the > tutorial located here: > > A Beginner's Introduction to Perl 5.10 - Perl.com > http://bit.ly/dHvsqC > (shortened to prevent cutoff) > > Instructions for the exercise: > "Given a month and the day of the week that's the first of that month, > print a calendar for the month." > > and would like to request feedback on the solution with regards to ways > I could potentially improve this code. Also any mention of perldocs or > other documentation to look over would be most appreciated. Thank you > ahead of time for any feedback. > > Note: Leap year logic is intentionally left out to make sure I > understood the basics.
OK. > > === Code === > use warnings; > use strict; > Nice. > use List::Util qw(first); > > my %days_in_months = ( > 'Jan' => 31, > 'Feb' => 29, > 'Mar' => 31, > 'Apr' => 30, > 'May' => 31, > 'Jun' => 30, > 'Jul' => 31, > 'Aug' => 31, > 'Sep' => 30, > 'Nov' => 31, > 'Dec' => 31 > ); 1. You should not be using one-space indentation, because the code is difficult to read that way. Either use tabs or use something like four spaces. > > my @days_of_the_week = ('Sun','Mon', 'Tue', 'Wed', 'Thu', 'Fri','Sat'); You can use qw() here: my @days_of_the_week = qw(Sun Mon Tue Wed Thu Fri Sat); See: * http://perlmeme.org/howtos/perlfunc/qw_function.html * http://perldoc.perl.org/functions/qw.html > > sub PrintHeaders { > print "Sun\tMon\tTue\tWed\tThu\tFri\tSat\n"; > } This can be written using (untested): print join("\t", @days_of_the_week), "\n"; And you can also use say in perl 5.10 and above. Furthermore, I see you're using CamelCase for functions. You should be using words_separated_by_underscore for that too. > > sub GetDayOfWeekIndex { > my $day_of_the_week_name = shift; > my $index_value = first { $days_of_the_week[$_] eq > $day_of_the_week_name } 0 .. $#days_of_the_week; > return $index_value; > } Maybe use a hash here instead. And the variables are too long and redundant for my taste. > > sub PrintCalendar { > my($month, $start_day) = @_; You should have a space between the "my" and the "(". > my $start_day_of_the_week = GetDayOfWeekIndex $start_day; > my $day_of_week_counter = 0; > > # If the first day of the week doesn't start on Sunday > # we will need to add some visual padding. > if($start_day_of_the_week != $day_of_week_counter) > { > # -1 is used here because we need to loop this for > # the days up to but not including the start day of > # the week > for my $i (0..($start_day_of_the_week-1)) > { > print "\t"; > } This can be done without a loop using the "x" operator": http://perldoc.perl.org/perlop.html#Multiplicative-Operators > > $day_of_week_counter = $start_day_of_the_week; > } > > for my $day (1..$days_in_months{$month}) > { > print $day; > > if($day_of_week_counter == $#days_of_the_week) > { It will be less confusing if you increment $day_of_week_counter before the conditional and check that it equals to @days_of_the_week in scalar context. > print "\n"; > $day_of_week_counter = 0; > } > else > { > print "\t"; > $day_of_week_counter++; > } > } ## end for > > } > > PrintHeaders; > PrintCalendar "Jan", "Fri"; > You should also make sure you print a trailing newline because otherwise some shells won't display the last line properly. Regards, Shlomi Fish > == Output == > Sun Mon Tue Wed Thu Fri Sat > 1 2 > 3 4 5 6 7 8 9 > 10 11 12 13 14 15 16 > 17 18 19 20 21 22 23 > 24 25 26 27 28 29 30 > 31 > > (Verified by looking at a calendar for January 2010 with Friday as the > first day of the week.) -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ What Makes Software Apps High Quality - http://shlom.in/sw-quality Chuck Norris can make the statement "This statement is false" a true one. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/