in reply to Re: Date plus Time sort from file
in thread Date plus Time sort from file

#!/usr/bin/perl -- use strict; use warnings; use Time::Piece; Main( @ARGV ); exit( 0 ); sub Main { my( $now ) = @_; ## $now ||= Time::Piece::localtime->strftime('%m-%d-%Y'); $now ||= "08-13-2013"; my $raw = 'Date Time Tag 08-13-2013 21:22:17 Yes 08-13-2013 22:22:17 No 08-14-2013 11:22:17 Yes 08-13-2012 21:22:17 Yes 08-13-2011 22:22:17 No 08-14-2012 01:22:17 Yes ';;;; my( $header, $todaytes ) = rubberBiscuit( $now , \$raw ); print $header; print "$_\n" for @$todaytes ; } sub rubberBiscuit { my( $now, $file ) = @_; use autodie; open my($in), '<', $file ; ## or die by autodie my @today; my $header = readline $in; while( readline $in ){ my($date, $time, $tag ) = split ' '; if( $date eq $now ){ push @today, join ' ', Time::Piece->strptime( "$date $time", '%m-%d-%Y %H:%M:%S', )->strftime('%Y-%m-%d %H:%M:%S'), $tag, ;;;;; } } @today = sort @today; return $header, \@today; } __END__ Date Time Tag 2013-08-13 21:22:17 Yes 2013-08-13 22:22:17 No

Replies are listed 'Best First'.
Re^3: Date plus Time sort from file
by boftx (Deacon) on Aug 15, 2013 at 05:28 UTC

    I think this can be simplified somewhat. The following just demonstrates the basic selection/sort logic without regard to how you want to handle the file itself.

    #!/usr/bin/perl use strict; use POSIX qw(strftime); my @orig_data = ( 'Date Time Tag', '08-13-2013 19:22:16 Yes', '08-13-2013 18:22:17 No', '08-13-2013 21:22:17 Yes', '08-13-2013 20:22:16 Yes', '08-13-2013 23:22:18 Yes', '08-13-2013 22:22:17 No', '08-14-2013 01:22:17 Yes', '08-14-2013 00:22:18 Yes', '08-14-2013 03:22:19 No', '08-14-2013 02:22:18 Yes', '08-14-2013 05:22:28 No', '08-14-2013 04:22:29 Yes', '08-14-2013 07:22:19 Yes', '08-14-2013 06:22:18 Yes', '08-14-2013 09:22:19 No', '08-14-2013 08:22:19 Yes', '08-14-2013 11:22:19 Yes', '08-14-2013 10:22:20 No', '08-14-2013 13:22:20 Yes', '08-14-2013 12:22:20 No', '08-14-2013 15:22:21 Yes', '08-14-2013 14:22:20 Yes', '08-14-2013 17:22:21 Yes', '08-14-2013 16:22:22 No', ); # this should be used instead of the hard-coded value below: # my $today = strftime("%m-%d-%Y",localtime()); my $today = '08-14-2013'; my @todays; for ( @orig_data ) { my ($date,$other) = split(/\s/,$_,2); next unless $date eq $today; push(@todays, "$other"); } # NOTE! this will reverse the order of Yes and No if they have the sam +e time. @todays = sort(@todays); for ( @todays ) { print "$today $_\n"; } __END__