Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: sub routine to sort per time

by steph_bow (Pilgrim)
on Nov 26, 2008 at 14:00 UTC ( [id://726106]=note: print w/replies, xml ) Need Help??


in reply to Re: sub routine to sort per time
in thread sub routine to sort per time

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

Replies are listed 'Best First'.
Re^3: sub routine to sort per time
by apl (Monsignor) on Nov 26, 2008 at 14:20 UTC
    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.

      Thanks apl ! Here is the updated code (still does not work), I get nothing from the print.

      #! 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; print STDOUT "a is $a\n"; print STDOUT "b is $b\n"; my $res; if ($a =~ /(\d\d):(\d\d)/){ $h_a = $1; $mn_a = $2; } else{ $res = 0; } if ($b =~ /(\d\d):(\d\d)/){ $h_a = $1; $mn_a = $2; } else{ $res = 0; } print STDOUT "h_a is $h_a\n"; print STDOUT "h_b is $h_b\n"; print STDOUT "mn_a is $mn_a\n"; print STDOUT "mn_b is $mn_b\n"; 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; } 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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 16:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found