in reply to Need help printing rectangle of asterisks (was: Beginner question)

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$\;}

Replies are listed 'Best First'.
Re: Re: Beginner question
by Popcorn Dave (Abbot) on May 24, 2002 at 21:34 UTC
    Maybe I'm picking nits here that need not be picked, but isn't it better to use chomp rather than chop after getting input data if all he wants to do is remove the \n?

    Not that I can think of a case right off the top of my head that it would cause this particular example to fail, but that's the way I leared it. :)

    If I'm somehow having a mental lapse here, please tell me.

    Some people fall from grace. I prefer a running start...

Re: Re: Beginner question
by Anonymous Monk on May 24, 2002 at 16:46 UTC
    Thanks for all your tips and information.