Correct me if I'm wrong, but this question of yours resembles an introductory Perl class assignment, my friend. Besides, the 'print a rectangle of specified width/height' is a classic problem for beginners. Frankly, monastery is not the place for you to ask questions pertaining to your home work which is supposed to be done on your own or with the help of your instructor (not any outside help, aka 'cheating'). However, noting your initial effort, I have included the 'UPDATES' below:

UPDATE: Arrgh, being a nice guy I am (*grin*) and seeing how you have at least tried something on your own and also that you were near to a solution, here's a quick fix for your problem:
print "Enter width\n"; chop($ans = <>); print "Enter height\n"; chop($ans2 = <>); for ($i=0;$i < $ans;$i++) { for ($j=0;$j < $ans2;$j++) { print "*"; } print "\n"; }
You simply had to nest the two looks to draw a nice rectangle ;)

UPDATE 1:

Since you are also a beginner, let me give you a few guiding hints as to the code in general. Give your variables appropriate self-documenting names. It's good to start doing this early in your career as a Perl 'hacker'. So, my suggestion for you would be to change your $ans and $ans2 variables to $height and $width respectively. You should also use 'strict' at all times! See my 'Oracle Speaketh' section on my homenode here vladb.
use strict; my ($width, $height); print "Enter width\n"; chop($width = <>); print "Enter height\n"; chop($height = <>); for (my $i = 0; $i < $height; $i++) { for (my $j = 0; $j < $width; $j++) { print "*"; } print "\n"; }


UPDATE 2: corrected my initial write up to remove arguable statements.

UPDATE 3: Definitely not a case of a 'mental lapse', Popcorn Dave ;-). In fact you've made a valid observation. However, I believe that in case when I use <> to retrieve user input I can always expect an extra '\n' at the end of a retrieved line as it is the default input separator (for the curious of mind, check documentation on the $/ variable in perlvar; also, <> operator won't return until it encounters the input separator character :) So, armed with this knowledge, I believe using chop() in place of chomp() is a bit faster (more elegant?) approach...

_____________________
$"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}

In reply to Re: Beginner question by vladb
in thread Need help printing rectangle of asterisks (was: Beginner question) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.