Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

sub routine to sort per time

by steph_bow (Pilgrim)
on Nov 26, 2008 at 10:26 UTC ( [id://726062]=perlquestion: print w/replies, xml ) Need Help??

steph_bow has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE

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
    If the format is just HH:MM you can simply use the normal string comparison, so a simple sort @dates should work.
    but it does not work
    That's not an error description.

    One of the obvious problems is that here:

    if ($h_a < $h_b){ -1; } if ($h_a > $h_b){ 1; }

    There should really be return statements. But all of this could be written rather compactly as $h_a <=> $h_b || $m_a <=> $m_b - but as I said before, for simple formats there's no need for that at all.

      Thanks a lot moritz, I updated the messages several times, sorry for the trouble

      However, I can't understand why $n is 0

      my %in; while (<$INFILE>){ m[(\d\d:\d\d) (.+)$] and push @{ $in{ $1 } }, $2 ; print STDOUT "1 is $1\n"; print STDOUT "2 is $2\n"; } my @keys = keys %in; my @times = sort @keys; foreach my $hr (@times) { print STDOUT "hr is $hr\n"; my $n = $#{ $in{ $hr } }; } __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

        Update: OP changed, so this might not be of relevance any more.

        I'm sorry, but I can't read your mind. I don't see a problem in the code you show, and stating that there is one doesn't help me to identify it.

        Is a cloaked ninja attacking you every time you run this program? If not, of what nature is that problem?

        If your output is not what you expect, you have to tell us what your input and your expectations are.

        Please read I know what I mean. Why don't you?.

        However, I can't understand why $n is 0
        Well, it's not always, at least not if I run your code and actually read from <DATA>. If there's one item in the array, it contains 0, which is the last index in the array. What do you want it to contain?
Re: sub routine to sort per time
by rovf (Priest) on Nov 26, 2008 at 11:12 UTC
    if ($a =~ /(\d\d):(\d\d)/){ $h_a = $1; $mn_a = 2; }

    In addition to what the others have said, shouldn't it be

    $mn_a = $2
    ?

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: sub routine to sort per time
by apl (Monsignor) on Nov 26, 2008 at 12:42 UTC
    Regardless of the rewrites suggested, did you do something as simple as putting in print statements to see if the code was doing what you thought it was doing?

    For example, my understanding is that $a and $b are magic variables used by sort. Did you ever print out what the routine received in those variables?

    A better coding style might be to pass those two values as parameters to the subroutine, and to store the result of the tests in a variable that would be passed in a return statement. Then you could display the returned value as well as the parameters, $h_a, $mn_a, etc.

      Thanks a lot apl

      Here is the program to take into account your remarks. However, there is still a problem.

      #! perl -slw use strict; use diagnostics; use Data::Dump qw[ pp ]; sub by_hour{ my ($a,$b) = @_; my $h_a; my $mn_a; my $h_b; my $mn_b; my $res; 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){ $res = -1; } if ($h_a > $h_b){ $res = 1; } if ($h_a == $h_b){ if ($mn_a < $mn_b){ $res = -1; } if ($mn_a > $mn_b){ $res =1; } if ($mn_a == $mn_b){ $res = 0; } } return $res; } my %in; while (<DATA>){ m[(\d\d:\d\d) (.+)$] and push @{ $in{ $1 } }, $2; } my @keys = keys %in; my @times = sort @keys; for my $hr ( sort by_hour keys %in ) { print STDOUT "$hr\n"; } __DATA__ 11:10 A1 12:30 E4 11:30 Z4 15:50 H5 12:02 H6 12:25 B2 11:25 A8 12:30 F3 14:30 E7 12:50 E15 12:55 E16
        You still don't have the print statements showing $a, $b, $h_a, $h_b, $mn_a, $mn_b, $res.

        Notice also that you're processing two strings even if they're not formatted correctly (that is, match neither of the =~ statements, the second of which should be an else.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://726062]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found