From a30723e36dc48cb6a0ef0d086f363482bf82105a Mon Sep 17 00:00:00 2001 From: spider-mario Date: Tue, 24 Jul 2012 17:13:01 +0200 Subject: Rewrote ponysaylist.pl. --- ponysaylist.pl | 71 +++++++++++++++++----------------------------------------- 1 file changed, 20 insertions(+), 51 deletions(-) diff --git a/ponysaylist.pl b/ponysaylist.pl index 401d83b..0e2db56 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; + +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 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; -- cgit From 0923d3c7274db0f75b9b6fab510513030c254427 Mon Sep 17 00:00:00 2001 From: spider-mario Date: Tue, 24 Jul 2012 16:57:21 +0200 Subject: Simplification of ponysaylinklist.pl as well. --- ponysaylinklist.pl | 78 +++++++++++++++--------------------------------------- 1 file changed, 21 insertions(+), 57 deletions(-) diff --git a/ponysaylinklist.pl b/ponysaylinklist.pl index ec6ff9b..27c507d 100755 --- a/ponysaylinklist.pl +++ b/ponysaylinklist.pl @@ -9,64 +9,28 @@ # 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 $argc = @ARGV; + +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"; + } } -- cgit From df1712c17c5560143ec64cc0823ffa02fd7561a8 Mon Sep 17 00:00:00 2001 From: spider-mario Date: Sat, 28 Jul 2012 12:20:46 +0200 Subject: The rewrite of ponysaylist.pl does not fail anymore with no parameters. --- ponysaylist.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ponysaylist.pl b/ponysaylist.pl index 0e2db56..0c0410c 100755 --- a/ponysaylist.pl +++ b/ponysaylist.pl @@ -16,7 +16,7 @@ use feature qw(say); use integer; use List::Util qw(max); -my $scrw = shift @ARGV; +my $scrw = shift @ARGV // 1; for (@ARGV) { # Format names from ponyies names @@ -24,7 +24,7 @@ for (@ARGV) { s/_(.*)/\t($1)/; } -my $maxw = max map {length} @ARGV; +my $maxw = max 1, map {length} @ARGV; my $cols = max 1, (($scrw + 2) / ($maxw + 2)); -- cgit From a42fe1fd4eb7fdb841d1668f8242051f3c9f5343 Mon Sep 17 00:00:00 2001 From: spider-mario Date: Sat, 28 Jul 2012 12:33:22 +0200 Subject: In ponysaylinklist.pl, $argc is not needed anymore. --- ponysaylinklist.pl | 1 - 1 file changed, 1 deletion(-) diff --git a/ponysaylinklist.pl b/ponysaylinklist.pl index 27c507d..4d34928 100755 --- a/ponysaylinklist.pl +++ b/ponysaylinklist.pl @@ -15,7 +15,6 @@ use utf8; use List::MoreUtils qw(natatime); my %hash; -my $argc = @ARGV; my $it = natatime 2, @ARGV; while (my ($source, $target) = &$it) { -- cgit