/
playground12.pl
executable file
·55 lines (38 loc) · 1.06 KB
/
playground12.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/perl
# alternative sorts for bc-sgrep.pl (testing)
#
require "/usr/local/lib/bclib.pl";
# sortcmp("b","a");
# sortcmp("a","b");
# sortcmp("c","c");
debug(sortcmp("5","0006"));
debug(sortcmp("5","0006","-n"));
debug(sortcmp("5","0006","-R"));
=item sortcmp($left,$right,$opts)
Compare $left and $right using "sort -$opts", returning:
-1 if $left < $right
+1 if $left > $right
0 if $left == $right
TODO: do comparisons in both directions to avoid sort weirdnesses
TODO: LC_ALL=C or something similar; perhaps -s?
=cut
sub sortcmp {
my($left,$right,$opts) = @_;
local(*A);
# shortcut if strings are byte-by-byte identical
if ($left eq $right) {return 0;}
# sort can behave oddly, both tests are required
# left then right
open(A, "|sort -c $opts 1> /tmp/out1.txt 2> /tmp/out2.txt");
print A "$left\n$right\n";
close(A);
my($l) = $?;
# right then left
open(A, "|sort -c $opts 1> /tmp/out1.txt 2> /tmp/out2.txt");
print A "$right\n$left\n";
close(A);
my($r) = $?;
if ($l && !$r) {return 1;}
if ($r && !$l) {return -1;}
return 0;
}