-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtable2
executable file
·45 lines (38 loc) · 984 Bytes
/
table2
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
#!/usr/bin/env perl
use strict;
use warnings;
my $idx = shift;
my $src = do { local $/; <> };
my @lines = grep { $_ ne '' } split /\r?\n/, $src;
my @border_offsets;
for my $x (0 .. length($lines[0]) - 1) {
if (guess_border($x, @lines)) {
push @border_offsets, $x;
}
}
my $row = @border_offsets - 1;
my @table = ('') x $row;
for my $i (0..$#border_offsets-1) {
my $start = $border_offsets[$i];
my $end = $border_offsets[$i+1];
for my $line (@lines) {
my $n = $end - $start - 1;
next if length($line) <= $n;
my $s = substr $line, $start + 1, $n;
$s =~ s/^[\x20]+|[\x20]+$//g;
$table[$i] .= $s;
}
}
if (defined $idx) {
print $table[$idx] . "\n";
} else {
print +(sort { length $b <=> length $a } @table)[0] . "\n";
}
sub guess_border {
my ($x, @lines) = @_;
for my $line (@lines) {
next if length($line) <= $x;
return 0 if substr($line, $x, 1) ne '|';
}
return 1;
}