In grouped_by_hour
below, for each line from the filehandle, if it has a timestamp and a name, we push
that name onto an array associated with the timestamp's hour, using sprintf
to normalize the hour in case one timestamp is 03:04:05
and another is 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;
}
The normalized hours also allow us to sort with the default comparison. The code below places the input in the special DATA
filehandle by having it after the __DATA__
token, but in real code, you might call 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
Output:
10:00:00 Bob
11:00:00 Tom, Fred, George
12:00:00 Bill