steph_bow has asked for the wisdom of the Perl Monks concerning the following question:
Sorry guys, I have found another way to get the results I wanted
I wanted to stock times inside slices of hours: all the times between, for ex: 11:00 and 12:00 and equally redistribute them inside this slice, but at the same time keeping the old values so as to make the difference between the new and former times Then I realized I had to use a second hash to conserve the minutes
This is the code which was already inspired a lot by BrowserUK but that I have uddated for my purpose.
#! perl -slw use strict; use diagnostics; use Data::Dump qw[ pp ]; my %in; my %former_in; while (<DATA>){ m[(\d\d):(\d\d) (.+)$] and push @{ $in{ $1 } }, $3 and $former_in{$3} += $2; print STDOUT "1 is $1\n"; print STDOUT "2 is $2\n"; print STDOUT "3 is $3\n"; } my @keys = keys %in; my @times = sort @keys; for my $hr ( sort{ $a <=> $b } keys %in ) { my $n = $#{ $in{ $hr } }; my $step = 60 / $n; my $min = 0; for my $flight ( @{ $in{ $hr } } ) { my $former_time = $former_in{$flight}; # relative difference my $diff = int( $min ) - $former_in{$flight}; printf( "%02d:%02d %s %s\n", $hr, int( $min ), $flight, $diff +); $min += $step; $min = 59 if $min > 59; } } __DATA__ 11:10 A1 11:30 E4 11:30 Z4 11:50 H5 12:02 H6 12:25 B2 12:25 A8 12:30 F3 12:30 E7 12:50 E15 12:55 E16
old prog
sub by_hour{ if ($a =~ /(\d\d):(\d\d)/){ $h_a = $1; $mn_a = $2; } if ($b =~ /(\d\d):(\d\d)/){ $h_a = $1; $mn_a = $2; } if ($h_a < $h_b){ -1; } if ($h_a > $h_b){ 1; } if ($h_a == $h_b){ if ($mn_a < $mn_b){ -1; } if ($mn_a > $mn_b){ 1; } if ($mn_a == $mn_b){ 0; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sub routine to sort per time
by moritz (Cardinal) on Nov 26, 2008 at 10:29 UTC | |
by steph_bow (Pilgrim) on Nov 26, 2008 at 10:49 UTC | |
by moritz (Cardinal) on Nov 26, 2008 at 10:53 UTC | |
by BrowserUk (Patriarch) on Nov 26, 2008 at 11:05 UTC | |
by moritz (Cardinal) on Nov 26, 2008 at 11:02 UTC | |
|
Re: sub routine to sort per time
by rovf (Priest) on Nov 26, 2008 at 11:12 UTC | |
|
Re: sub routine to sort per time
by apl (Monsignor) on Nov 26, 2008 at 12:42 UTC | |
by steph_bow (Pilgrim) on Nov 26, 2008 at 14:00 UTC | |
by apl (Monsignor) on Nov 26, 2008 at 14:20 UTC | |
by steph_bow (Pilgrim) on Nov 26, 2008 at 14:49 UTC |