Merge pull request #57 from spider-mario/master

Rewrite of Perl scripts
This commit is contained in:
Mattias Andrée 2012-07-28 05:08:37 -07:00
commit 8bf68d139e
2 changed files with 40 additions and 108 deletions

View file

@ -9,63 +9,26 @@
# Author: Mattias Andrée, maandree@kth.se # Author: Mattias Andrée, maandree@kth.se
%hash = (); use strict;
$argc = @ARGV; use warnings;
use utf8;
use List::MoreUtils qw(natatime);
$i = 0; my %hash;
while ($i < $argc)
{
$source = $ARGV[$i];
$i += 1;
$target = $ARGV[$i];
$i += 1;
if ($source eq $target)
{
$hash{$source} = [ () ];
}
}
$i = 0; my $it = natatime 2, @ARGV;
while ($i < $argc) while (my ($source, $target) = &$it) {
{ unless ($source eq $target) {
$source = $ARGV[$i];
$i += 1;
$target = $ARGV[$i];
$i += 1;
unless ($source eq $target)
{
push @{$hash{$target}}, $source; push @{$hash{$target}}, $source;
} }
} }
$i = 0; $it = natatime 2, @ARGV;
while ($i < $argc) while (my ($source, $target) = &$it) {
{ if ($source eq $target) {
$source = $ARGV[$i]; my @list = @{$hash{$source} // []};
$i += 1;
$target = $ARGV[$i];
$i += 1;
if ($source eq $target)
{
@list = @{ $hash{$source} };
$first = 1;
print $source; print $source;
foreach $link (@list) print ' (', join(' ', @list), ')' if @list;
{
if ($first eq 1)
{
print " (".$link;
$first = 0;
}
else
{
print " ".$link;
}
}
if ($first eq 0)
{
print ")";
}
print "\n"; print "\n";
} }
} }

View file

@ -1,4 +1,4 @@
#!/usr/bin/perl #!/usr/bin/env perl
# ponysaylist # ponysaylist
# Prints a list of ponies in columns # Prints a list of ponies in columns
@ -9,63 +9,32 @@
# Author: Mattias Andrée, maandree@kth.se # Author: Mattias Andrée, maandree@kth.se
$first = 1; use strict;
$scrw = 1; use warnings;
$maxw = 1; use utf8;
use feature qw(say);
use integer;
use List::Util qw(max);
foreach $arg (@ARGV) my $scrw = shift @ARGV // 1;
{
for (@ARGV) {
# Format names from ponyies names # Format names from ponyies names
$arg =~ s/([a-z])([A-Z])/\1 \2/; s/(?<=[a-z])(?=[A-Z])/ /;
#$arg =~ s/_(.*)/\t(\1)/; ## Incompatible with `ponysay -L` s/_(.*)/\t($1)/;
if ($first == 1)
{ $first = 0;
$scrw = $arg;
}
else
{ $w = length $arg;
$maxw = $w if ($w > $maxw);
}
} }
$cols = int (($scrw + 2) / ($maxw + 2)); my $maxw = max 1, map {length} @ARGV;
$cols = 1 if ($cols < 1);
my $cols = max 1, (($scrw + 2) / ($maxw + 2));
@list = (); my @list = map {sprintf "%-${maxw}s", $_} @ARGV;
$first = 1; my $rows = (@list + $cols - 1) / $cols;
$items = 0;
foreach $arg (@ARGV) my @rowlist;
{ for my $i (0 .. $#list) {
if ($first == 1) push @{$rowlist[$i % $rows]}, $list[$i];
{ $first = 0;
}
else
{ $ws = $maxw - (length $arg);
push @list, $arg.(" "x$ws);
$items += 1;
}
}
$rows = int (($items + $cols - 1) / $cols);
$i = 0;
@rowlist = ();
while ($i < $items)
{ $row = 0;
while (($row < $rows) and ($i < $items))
{
$rowlist[$row] .= " " unless ($i < $rows);
$rowlist[$row] .= $list[$i];
$row += 1;
$i += 1;
} }
foreach $row (@rowlist)
{
print $row."\n";
} }
say join ' ', @$_ for @rowlist;