A completely different approach that turns out to be far simpler and more robust. (Ie. It actually works for all possibilities:)
In addition, it avoids a bunch of convoluted range comparisons and the need for sorting, whilst producing fully sorted output. A win-win-win for going back to the drawing board.
#! perl -slw use strict; use Data::Dump qw[ pp ]; sub dhm2int { my( $d, $h, $m ) = unpack '(A2)*', $_[0]; return ( ( $d - 1 ) * 24 + $h ) * 60 + $m; } sub int2dhm { sprintf "%02d%02d%02d", int($_[0]/1440)+1, int($_[0]/60)%24, $_[0] +%60; } #my @listone = (['010000','010010',2],['010200','010210',5],['012359','020001',3] +); #my @listtwo = (['010005','010015',1],['010207','010211',4]); my @listone = (['010000','010110',6],['010200','010210',5],['012350','020012',3] +); my @listtwo = (['010005','010015',1],['010207','010211',4],['012355','020003',1] +); my @expd = map { my $in = $_; my $s = dhm2int( $in->[ 0 ] ); my $e = dhm2int( $in->[ 1 ] ); my @out; while( int( $s / 1440 ) != int( $e / 1440 ) ) { my $newe = ( int( $s / 1440 ) +1 ) * 1440 -1; push @out, [ int2dhm( $s ), int2dhm( $newe ), $in->[2], $s, $n +ewe ]; $s = $newe +1; } ( @out, [ int2dhm( $s ), $in->[1], $in->[2], $s, $e ] ); } @listone, @listtwo; my( @tally, @id ); for my $e ( 0 .. $#expd ) { my $r = $expd[ $e ]; for my $i ( $r->[ 3 ] .. $r->[ 4 ] ) { if( !defined( $tally[ $i ] ) or $tally[ $i ] > $r->[ 2 ] ) { $tally[ $i ] = $r->[ 2 ]; $id[ $i ] = $e; } } } my @res; my $i = 0; while( $i < $#id ) { ++$i until defined $id[ $i ]; my $id = $id[ $i ]; my $start = $i; ++$i while defined( $id[ $i ] ) and $id[ $i ] == $id; my $end = $i - 1; push @res, [ int2dhm( $start ), int2dhm( $end ), $tally[ $start ] +]; } pp \@res; __END__ C:\test>935755.pl [ ["010000", "010004", 2], ["010005", "010015", 1], ["010200", "010206", 5], ["010207", "010211", 4], ["012359", "012359", 3], ["020000", "020001", 3], ] C:\test>935755.pl [ ["010000", "010004", 2], ["010005", "010015", 1], ["010200", "010206", 5], ["010207", "010211", 4], ["012359", "012359", 3], ["020000", "020001", 3], ]
In reply to Re: Date Array Convolution
by BrowserUk
in thread Date Array Convolution
by alanonymous
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |