- 论坛徽章:
- 7
|
给个:
help:
- # perl abc.pl data # for terminal only
- # perl abc.pl -s data
- # -l sort lexically perl abc.pl -l data
- # -n sort numerically perl abc.pl -n data
- # -s for save perl abc.pl -s data > save
- # OK perl abc,pl -n -s > save
- # OK perl abc,pl -l -s > save
- # Don't do it # perl abc,pl data > save
- # Don't do it # perl abc,pl -n data > save
- # Don't do it # perl abc,pl -l data > save
复制代码- #!/usr/bin/perl
- use 5.018;
- my ( $F, %H, %L, $S, %O ) = pop;
- my @LP = ( '+-- ', '| ', ' ', '|-- ', qr/^[^\s+\|]/ );
- my @LT = ( '└── ', '│ ', ' ', '├── ', qr/^[^\s└│]/ );
- my @LH = qw/M L N H R/;
- my %S = ( l => 'cmp', n => '<=>' );
- map { /-([snl])/ and $1 eq 's' ? $O{$1}++ : ( $O{S} = $1 ) } @ARGV;
- @ARGV = ($F);
- @L{@LH} = $O{s} ? @LP : @LT;
- sub L {
- my ( $h, $k ) = @_;
- return [$k] unless defined $h->{$k};
- map [ $k, @$_ ], map L( $h->{$k}, $_ ), keys %{ $h->{$k} }}
- sub F {
- my ( $i, $j, $P ) = @_;
- $P->[$i][$j] ne $L{N} and $P->[$i-1][$j] eq $L{M}
- and $P->[$i-1][$j] = $L{H} }
- sub S { my $max = shift;
- eval 'sub { '
- . join( '||', map '$a->['.$_.']'.$S{$O{S}}.'$b->['."$_]", 1 .. $max - 1 )
- . '}'; }
- map { delete $H{$_} }
- map { $H{ $_->[0] }{ $_->[1] } = $H{ $_->[1] }; $_->[1] }
- map { my ( $A, $B ) = ( split( /[-\n]/ ), '' );
- $H{$A}{$B} = undef; [ $A, $B ] } <>;
- for my $k ( sort keys %H ) {
- my ( $end, $max, @a, @P, %h ) = ( 0, 0, L \%H, $k );
- if ( $O{S} ){
- $max < @$_ and $max = @$_ for @a;
- my $it = S $max;
- @a = sort $it @a; }
- for my $i (@a) {
- for my $j ( 0 .. $#{$i} ) {
- $i->[$j] or next;
- $h{$j} and $h{$j} eq $i->[$j] and next;
- $P[@P][$j] = $i->[$j];
- $h{$j} = $i->[$j]; }}
- $P[$_][1] and $end = $_ and last for reverse 0 .. $#P;
- for my $i ( 0 .. $#P ) {
- for my $j ( 0 .. $#{ $P[$i] } - 1 ) {
- $j or $P[$i][$j] = $P[$i][$j+1] ? $L{M} : $i <= $end
- ? $L{L} : $L{N} and F $i, $j, \@P and next;
- my ( $G, $T, $H ) = ( 0, 0 );
- $P[$_][$j+1] and $G = $_ and last for $i + 1 .. $#P;
- $H = ( $G and $j < $#{ $P[$G-1] } ) ? $L{L} : $L{N};
- $P[$i][$j] = $P[$i][$j+1] ? $L{M} : $H;
- F $i, $j, \@P; }}
- say map { ( /$L{R}/ ? $L{H} : $L{L} ) . $_ } join '', @$_ for @P; }
复制代码
|
|