aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2012-07-28 05:08:37 -0700
committerMattias Andrée <maandree@operamail.com>2012-07-28 05:08:37 -0700
commit8bf68d139ec78f6cd7a019b66f4ffad7045c27a4 (patch)
treefc456e98b9e13a272250b2e176ce6f8bbaca508b
parent365a35b90ee1cab9a19f2f121496b37bfe1ed470 (diff)
parenta42fe1fd4eb7fdb841d1668f8242051f3c9f5343 (diff)
downloadponysay-8bf68d139ec78f6cd7a019b66f4ffad7045c27a4.tar.gz
ponysay-8bf68d139ec78f6cd7a019b66f4ffad7045c27a4.tar.bz2
ponysay-8bf68d139ec78f6cd7a019b66f4ffad7045c27a4.zip
Merge pull request #57 from spider-mario/master
Rewrite of Perl scripts
-rwxr-xr-xponysaylinklist.pl77
-rwxr-xr-xponysaylist.pl71
2 files changed, 40 insertions, 108 deletions
diff --git a/ponysaylinklist.pl b/ponysaylinklist.pl
index ec6ff9b..4d34928 100755
--- a/ponysaylinklist.pl
+++ b/ponysaylinklist.pl
@@ -9,64 +9,27 @@
# Author: Mattias Andrée, maandree@kth.se
-%hash = ();
-$argc = @ARGV;
-
-$i = 0;
-while ($i < $argc)
-{
- $source = $ARGV[$i];
- $i += 1;
- $target = $ARGV[$i];
- $i += 1;
- if ($source eq $target)
- {
- $hash{$source} = [ () ];
- }
-}
-
-$i = 0;
-while ($i < $argc)
-{
- $source = $ARGV[$i];
- $i += 1;
- $target = $ARGV[$i];
- $i += 1;
- unless ($source eq $target)
- {
- push @{ $hash{$target} }, $source;
- }
+use strict;
+use warnings;
+use utf8;
+use List::MoreUtils qw(natatime);
+
+my %hash;
+
+my $it = natatime 2, @ARGV;
+while (my ($source, $target) = &$it) {
+ unless ($source eq $target) {
+ push @{$hash{$target}}, $source;
+ }
}
-$i = 0;
-while ($i < $argc)
-{
- $source = $ARGV[$i];
- $i += 1;
- $target = $ARGV[$i];
- $i += 1;
- if ($source eq $target)
- {
- @list = @{ $hash{$source} };
- $first = 1;
- print $source;
- foreach $link (@list)
- {
- if ($first eq 1)
- {
- print " (".$link;
- $first = 0;
- }
- else
- {
- print " ".$link;
- }
- }
- if ($first eq 0)
- {
- print ")";
- }
- print "\n";
- }
+$it = natatime 2, @ARGV;
+while (my ($source, $target) = &$it) {
+ if ($source eq $target) {
+ my @list = @{$hash{$source} // []};
+ print $source;
+ print ' (', join(' ', @list), ')' if @list;
+ print "\n";
+ }
}
diff --git a/ponysaylist.pl b/ponysaylist.pl
index 401d83b..0c0410c 100755
--- a/ponysaylist.pl
+++ b/ponysaylist.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# ponysaylist
# Prints a list of ponies in columns
@@ -9,63 +9,32 @@
# Author: Mattias Andrée, maandree@kth.se
-$first = 1;
-$scrw = 1;
-$maxw = 1;
+use strict;
+use warnings;
+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
- $arg =~ s/([a-z])([A-Z])/\1 \2/;
- #$arg =~ s/_(.*)/\t(\1)/; ## Incompatible with `ponysay -L`
-
- if ($first == 1)
- { $first = 0;
- $scrw = $arg;
- }
- else
- { $w = length $arg;
- $maxw = $w if ($w > $maxw);
- }
+ s/(?<=[a-z])(?=[A-Z])/ /;
+ s/_(.*)/\t($1)/;
}
-$cols = int (($scrw + 2) / ($maxw + 2));
-$cols = 1 if ($cols < 1);
-
-
-@list = ();
-
-$first = 1;
-$items = 0;
-foreach $arg (@ARGV)
-{
- if ($first == 1)
- { $first = 0;
- }
- else
- { $ws = $maxw - (length $arg);
- push @list, $arg.(" "x$ws);
- $items += 1;
- }
-}
+my $maxw = max 1, map {length} @ARGV;
+my $cols = max 1, (($scrw + 2) / ($maxw + 2));
-$rows = int (($items + $cols - 1) / $cols);
-$i = 0;
-@rowlist = ();
+my @list = map {sprintf "%-${maxw}s", $_} @ARGV;
-while ($i < $items)
-{ $row = 0;
- while (($row < $rows) and ($i < $items))
- {
- $rowlist[$row] .= " " unless ($i < $rows);
- $rowlist[$row] .= $list[$i];
- $row += 1;
- $i += 1;
-} }
+my $rows = (@list + $cols - 1) / $cols;
-foreach $row (@rowlist)
-{
- print $row."\n";
+my @rowlist;
+for my $i (0 .. $#list) {
+ push @{$rowlist[$i % $rows]}, $list[$i];
}
+say join ' ', @$_ for @rowlist;