#!/usr/bin/perl use strict; my %hash; while( ) { chomp; my( $name, @times ) = split; # convert each time to [ date, time bin ] @times = map { [ date($_), time_bin( $_ ) ] } @times; =pod The multi-level hash looks like this: $hash{ date }{ time bin }{ queue name }{'running'} {'queued'} Look at it with: use Data::Dumper; print Dumper( \%hash ); =cut # count the bin it was queued, even if it runs in that bin $hash{ $times[0][0] }{ $times[0][1] }{ $name }{'queued'}++; # count the bin it starts running $hash{ $times[1][0] }{ $times[1][1] }{ $name }{'running'}++; # count the bin it ended, unless it's the same bin it started # might want to check that it starts and ends on the same date too $hash{ $times[2][0] }{ $times[2][1] }{ $name }{'running'}++ if $times[1][1] ne $times[2][1]; } #### { DATE: foreach my $date ( sort keys %hash ) { print_header( $date ); my $hour_hash = $hash{ $date }; HOUR: foreach my $hour ( sort { $a <=> $b } keys %$hour_hash ) { my $queue_hash = $hour_hash->{$hour}; my $string = ''; QUEUE: foreach my $queue ( sort keys %$queue_hash ) { $string .= sprintf " %6s %4d %4d\n", $queue, $queue_hash->{$queue}{'queued'}, $queue_hash->{$queue}{'running'}; } # add the hour last to avoid a special case substr( $string, 0, 5 ) = $hour; print $string, "-" x 54, "\n"; } } } #### sub time_bin { # this just assumes an hour, such as 19. # adjust for whatever time bin you'd like to use sprintf "%02d:00", (localtime( $_[0] ) )[2]; } sub date { my @times = localtime( $_[0] ); $times[5] += 1900; $times[4] += 1; # join on / and make two digits for easy sorting return join "/", map { sprintf "%02d", $_ } @times[3,4,5]; } sub print_header { print <<"HERE"; Date: $_[0] Time Queue Queued Running ------------------------------------------------------ HERE } #### Date: 23/10/2006 Time Queue Queued Running ------------------------------------------------------ 08:00 queuea 1 0 ------------------------------------------------------ 09:00 queuea 0 1 queueb 1 0 ------------------------------------------------------ 11:00 queuec 1 1 queued 1 1 ------------------------------------------------------ 13:00 queuea 1 0 queueb 2 2 queued 1 0 ------------------------------------------------------ 14:00 queuea 1 1 queuec 1 1 ------------------------------------------------------ 15:00 queuea 1 1 queueb 5 5 queuec 1 1 ------------------------------------------------------ 16:00 queuea 1 0 queueb 2 2 queued 5 5 ------------------------------------------------------ 17:00 queued 3 3 ------------------------------------------------------ 18:00 queuea 0 1 ------------------------------------------------------ 19:00 queuea 0 1 queueb 0 2 queued 0 4 ------------------------------------------------------ 20:00 queueb 0 5 queuec 0 1 queued 0 5 ------------------------------------------------------ 21:00 queuea 0 2 queueb 0 3 queuec 0 1 ------------------------------------------------------ 23:00 queued 0 1 ------------------------------------------------------ Date: 24/10/2006 Time Queue Queued Running ----------------------------------------------------- 01:00 queuea 0 1 queuec 0 1 ------------------------------------------------------ 02:00 queueb 0 1 ------------------------------------------------------ 10:00 queuea 0 1 ------------------------------------------------------