in reply to sub routine to sort per time

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.

Replies are listed 'Best First'.
Re^2: sub routine to sort per time
by steph_bow (Pilgrim) on Nov 26, 2008 at 10:49 UTC

    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?