M
Mumia W. (reading news)
To reduce my boredom I took grocery_stocker's data from "At a loss how
to sort this file," and I wrote a program to sort it:
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Date:arse;
6 my $reverser = -1;
7
8 my $sortfn = sub {
9 my ($c, $d) = map str2time(substr($_, 43, 16)), ($a, $b);
10 $reverser * ($c <=> $d);
11 };
12
13 @ARGV = 'sort-long-dates.txt';
14 my @data = map uc $_, grep /./, <>;
15 my @sorted = sort $sortfn @data;
16 print join("",@sorted), "\n";
17
The data is sorted as expected, but something strange happens:
Date:arse::str2time lowercases the weekday names. I deliberately
capitalize everything on the line to show that only the dates are affected.
I sort of know why this is happening. The value returned by substr() is
tied to the portion of the string it came from, and str2time() probably
lowercases its input string before doing any other processing.
My question is, "why are the dates lowercased only when line 10 uses
the$reverser variable?"
I wanted to be able to change my program's sort order (ascending,
descending) by only changing one variable, so I created $reverser;
however, when I use the $reverser, the dates are converted to lowercase,
but when I do not use the $reverser, and line 10 reads "$d <=> $c", the
dates are not modified.
I can't see the relationship between the str2time() on line 9 and the
use of $reverser on line 10.
BTW, the dates are not modified if the $reverser variable is defined
within the $sortfn subroutine, e.g.
7 my $sortfn = sub {
8 my $reverser = -1;
9 my ($c, $d) = map str2time(substr($_, 43, 16)), ($a, $b);
10 $reverser * ($c <=> $d);
11 };
What's going on?
-------------------------------
Perl 5.8.4
Debian GNU/Linux 3.1
Date:arse 2.27
--
(e-mail address removed)
lar ttyp2 216.106.179.129 Fri Nov 17 17:12 - 17:14 (00:01)
lar ttypa 216.106.179.129 Fri Nov 17 15:53 - 15:55 (00:01)
lar ttypp 216.106.179.129 Thu Nov 16 17:11 - 17:21 (00:09)
lar ttypk 216.106.179.129 Thu Nov 16 14:20 - 14:21 (00:01)
lar ttypn 216.106.179.129 Thu Nov 16 13:23 - 13:37 (00:13)
irongeek ttypi 216.106.179.129 Wed Nov 15 17:27 - 17:32 (00:04)
sabre ttyp5 216.106.179.129 Wed Nov 15 13:59 - 14:03 (00:04)
lar ttyp5 216.106.179.129 Wed Nov 15 13:57 - 13:59 (00:01)
sabre ttyp5 216.106.179.129 Wed Nov 15 13:28 - 13:57 (00:28)
sabre ttypc 216.106.179.129 Wed Nov 15 12:10 - 12:10 (00:00)
lar ttypd 71.57.146.22 Fri Nov 17 07:27 - 07:43 (00:16)
irongeek ttyp2 71.57.146.22 Thu Nov 16 07:49 - 07:56 (00:07)
sabre ttypg 71.57.146.22 Sat Nov 11 15:56 - 16:09 (00:12)
to sort this file," and I wrote a program to sort it:
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Date:arse;
6 my $reverser = -1;
7
8 my $sortfn = sub {
9 my ($c, $d) = map str2time(substr($_, 43, 16)), ($a, $b);
10 $reverser * ($c <=> $d);
11 };
12
13 @ARGV = 'sort-long-dates.txt';
14 my @data = map uc $_, grep /./, <>;
15 my @sorted = sort $sortfn @data;
16 print join("",@sorted), "\n";
17
The data is sorted as expected, but something strange happens:
Date:arse::str2time lowercases the weekday names. I deliberately
capitalize everything on the line to show that only the dates are affected.
I sort of know why this is happening. The value returned by substr() is
tied to the portion of the string it came from, and str2time() probably
lowercases its input string before doing any other processing.
My question is, "why are the dates lowercased only when line 10 uses
the$reverser variable?"
I wanted to be able to change my program's sort order (ascending,
descending) by only changing one variable, so I created $reverser;
however, when I use the $reverser, the dates are converted to lowercase,
but when I do not use the $reverser, and line 10 reads "$d <=> $c", the
dates are not modified.
I can't see the relationship between the str2time() on line 9 and the
use of $reverser on line 10.
BTW, the dates are not modified if the $reverser variable is defined
within the $sortfn subroutine, e.g.
7 my $sortfn = sub {
8 my $reverser = -1;
9 my ($c, $d) = map str2time(substr($_, 43, 16)), ($a, $b);
10 $reverser * ($c <=> $d);
11 };
What's going on?
-------------------------------
Perl 5.8.4
Debian GNU/Linux 3.1
Date:arse 2.27
--
(e-mail address removed)
lar ttyp2 216.106.179.129 Fri Nov 17 17:12 - 17:14 (00:01)
lar ttypa 216.106.179.129 Fri Nov 17 15:53 - 15:55 (00:01)
lar ttypp 216.106.179.129 Thu Nov 16 17:11 - 17:21 (00:09)
lar ttypk 216.106.179.129 Thu Nov 16 14:20 - 14:21 (00:01)
lar ttypn 216.106.179.129 Thu Nov 16 13:23 - 13:37 (00:13)
irongeek ttypi 216.106.179.129 Wed Nov 15 17:27 - 17:32 (00:04)
sabre ttyp5 216.106.179.129 Wed Nov 15 13:59 - 14:03 (00:04)
lar ttyp5 216.106.179.129 Wed Nov 15 13:57 - 13:59 (00:01)
sabre ttyp5 216.106.179.129 Wed Nov 15 13:28 - 13:57 (00:28)
sabre ttypc 216.106.179.129 Wed Nov 15 12:10 - 12:10 (00:00)
lar ttypd 71.57.146.22 Fri Nov 17 07:27 - 07:43 (00:16)
irongeek ttyp2 71.57.146.22 Thu Nov 16 07:49 - 07:56 (00:07)
sabre ttypg 71.57.146.22 Sat Nov 11 15:56 - 16:09 (00:12)