Multiplication tables: Difference between revisions

Content added Content deleted
(→‎{{header|Perl}}: Removed more magic numbers. Removed shebang line and "use strict", since they're implied. Switched to the statement-modifier form of "foreach" where possible, for readability.)
Line 224: Line 224:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<lang perl>our $max = 12;
our $width = length($max**2) + 1;
use strict;
our $max = 12;


printf "%${width}s", $_ foreach 'x|', 1..$max;
print " x|";
print "\n", '-' x ($width - 1), '+', '-' x ($max*$width), "\n";
foreach my $a (1..$max) { printf(" %3d", $a); }
print "\n---+", "-" x ($max*4), "\n";
foreach my $i (1..$max) {
foreach my $i (1..$max) {
printf (" %2d", $i) ;
printf "%${width}s", $_
foreach "$i|", map { $_ >= $i and $_*$i } 1..$max;
print "|";
foreach my $j (1..$max) {
if ($j >= $i) { printf(" %3d", $j*$i); }
else { print " "; }
}
print "\n";
print "\n";
}</lang>
}</lang>

Output:
Output:

<pre>
<pre>
x| 1 2 3 4 5 6 7 8 9 10 11 12
x| 1 2 3 4 5 6 7 8 9 10 11 12