#!/usr/bin/perl -w use strict; use Date::Calc qw(Day_of_Year); # Format of monkdays.txt # marto:01-28:11-30 # :: my $monkfile = "monkdays.txt"; my %monks; open MONKS, "<$monkfile" or die "Ugh:$!\n"; while () { chomp; my ($nick, $joined, $bday) = split(/:/, $_); $monks{$nick}{joined} = $joined; $monks{$nick}{bday} = $bday; # Calculate the julian day for each date, and add to hash $monks{$nick}{jday_joined} = Day_of_Year("2006", split(/-/, $joined)); $monks{$nick}{jday_bday} = Day_of_Year("2006", split(/-/, $bday)); # Now, calculate the delta (in days) between birthdate and joined date $monks{$nick}{delta} = ($monks{$nick}{jday_joined} > $monks{$nick}{jday_bday}) ? ($monks{$nick}{jday_joined} - $monks{$nick}{jday_bday}) : ($monks{$nick}{jday_bday} - $monks{$nick}{jday_joined}); } close MONKS; print "Total Monks with birthdays:", scalar (keys %monks), "\n\n"; print "Monks who joined within 10 days of their birthday:\n\n"; printf "%-20s %-10s %-10s %-5s", "Monk", "Joined", "Birthday", "Delta"; print "\n"; foreach my $monk (sort by_delta keys %monks) { last if $monks{$monk}{delta} > 10; printf "%-20s %-10s %-10s %-5s", "$monk", "$monks{$monk}{joined}", "$monks{$monk}{bday}", "$monks{$monk}{delta}"; print "\n"; } sub by_delta { return $monks{$a}{delta} <=> $monks{$b}{delta}; }