You are not logged in.

#1 2011-10-09 19:32:10

pizet
Member
From: Slovak Republic
Registered: 2011-06-18
Posts: 18

Shortening a perl script

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

#2 2011-10-09 20:38:20

jetm
Member
From: Costa Rica
Registered: 2010-12-19
Posts: 34

Re: Shortening a perl script

$ perl -w -mstrict -e 'for$-(1..10) { printf "%4d", $_*$-for 1..10; print$/ }'

Offline

#3 2011-10-09 22:05:59

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,324

Re: Shortening a perl script

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

Board footer

Powered by FluxBB