grouped_by_hour , , push , , sprintf 03:04:05, - 3:9:18.
sub grouped_by_hour {
my($fh) = @_;
local $_;
my %hour_names;
while (<$fh>) {
push @{ $hour_names{sprintf "%02d", $1} } => $2
if /^(\d+):\d+:\d+\s+(.+?)\s*$/;
}
wantarray ? %hour_names : \%hour_names;
}
Normalized clocks also allow you to sort by default. The code below places the input in a special file descriptor DATA, having it after the token __DATA__, but in real code you can call it grouped_by_hour $fh.
my %hour_names = grouped_by_hour \*DATA;
foreach my $hour (sort keys %hour_names) {
print "$hour:00:00 ", join(", " => @{ $hour_names{$hour} }), "\n";
}
__DATA__
10:00:00 Bob
11:00:00 Tom
11:00:20 Fred
11:00:40 George
12:00:00 Bill
Conclusion:
10:00:00 Bob
11:00:00 Tom, Fred, George
12:00:00 Bill
source
share