You are not logged in.
Pages: 1
Topic closed
Hello.
I have a task to write a script in perl which prints a 10 x 10 grid, where on position (i,j) is number i*j. The point is, that I should make the script as short as possible. The numbers should also be aligned to the right margin. My version is:
#!/usr/bin/perl
use strict;
use warnings;
printf(" 0 1 2 3 4 5 6 7 8 9 10\n");
for my $i (0 ... 10) {
printf("%2d", $i);
for my $j (0 ... 10) {
printf("%4d", $i * $j);
}
print("\n");
}
It has to work with "use strict;" and "use warnings;".
Do you have any suggestions how to make it shorter?
Do you follow my way? Or just see a black stain swimming in the Milky Way...
Offline
$ perl -w -mstrict -e 'for$-(1..10) { printf "%4d", $_*$-for 1..10; print$/ }'
Offline
This has the look and feel of a homework assignment, and is therefore in viloation of our policy on homework
I am closing this thread. If I am in error, contact me by email and convince me.
ewaller
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Pages: 1
Topic closed