#!/usr/bin/perl use warnings; use strict; # Write a script to print 11x11 multiplication table, only the top half triangle. # Default table to 11x11, will work to 32x32 then formatting breaks down. my $d = shift || 11; usage() if ($d > 32 or $d < 1); head_print($d); body_print($d); sub head_print { my $d = shift; print " x|"; print sprintf "%4s", $_ foreach (1 .. $d); print "\n---+", "----" x $d, "\n"; } sub body_print { my $d = shift; my $seen = {}; foreach my $r (1 .. $d) { foreach my $c (1 .. $d) { print sprintf "%4s", "$r|" if ($c == 1); if ($seen->{$c}{$r}) { print sprintf "%4s", " "; } else { $seen->{$r}{$c}++; print sprintf "%4s", $r*$c; } } print "\n"; } } sub usage { print <