Good morning monks. Today I figured I'd post this little script I wrote. It's not that cool and you can propbably point out errors in my writing/syntax style, so feel free to yell at me :)

#!/usr/bin/perl # Put on your nerd glasses and draw a square! use strict; use warnings; my $balancex = 10; # width my $repeatx = $balancex; #don't change repeatx!!! use balancex instead +. my $repeaty = 10; # height do{ while($repeatx > 0){ print ". "; # change the period to print another character, but ke +ep the extra space. $repeatx -= 1; } print "\n"; $repeatx += $balancex; $repeaty -= 1; } until ($repeaty == 0); #corrected by Athanasius & AppleFritter

No doubt this could be done in much fewer lines, or even as a one-liner :P But as it says it draws a square with periods, and is just fun to look at.

Replies are listed 'Best First'.
Re: Draw a Square With Perl!
by Athanasius (Archbishop) on Aug 14, 2014 at 15:09 UTC

    Hello Dipseydoodle,

    The only real error I can see is using eq to compare numbers. Use == instead here:

    } until ($repeaty == 0);

    Apart from that, the code performs as advertised. But yes, it can be done more simply (and more Perlishly). For example*:

    #! perl use strict; use warnings; use constant { WIDTH => 10, HEIGHT => 10, CHAR => '.', }; for (1 .. HEIGHT) { print CHAR . ' ' for 1 .. WIDTH; print "\n"; }

    Update: *Generalising a square to a rectangle.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Draw a Square With Perl! (golf)
by toolic (Bishop) on Aug 14, 2014 at 15:14 UTC
    or even as a one-liner
    perl -E'say". "x10for 1..10'
Re: Draw a Square With Perl!
by AppleFritter (Vicar) on Aug 14, 2014 at 16:28 UTC

    Neat, although as has been pointed out, it can indeed be condensed a lot.

    } until ($repeaty ==); #corrected by Athamasius

    You forgot the zero after the == there -- and Athanasius may object to the non-standard spelling of his name, too. :)

Re: Draw a Square With Perl!
by Dipseydoodle (Acolyte) on Aug 14, 2014 at 15:19 UTC

    Hmm, yes I do see that. Why did I use eq? Thanks, that one liner and the above posts are cool!