Help can't figure this one out
I have written following coding to produce a triangle pattern(see below); I now want to produce following paterns I can't figer this out (a)(b) (c) (d) diamond ** * * * * * * * * * * * * * * * ** * * ** * * * * * * * ** * * * * * * * * * * * ** * * * * * * * * * * * * ** * * * * * * * __ #!/usr/local/bin/perl -w my $num_rows; my $i; my $r; $num_rows = ; for ($r = 1; $r <= $num_rows; $r++) { # indent by printing num_rows - r spaces for ($i = 1; $i <= $num_rows - $r; $i++) {print (" \n");} # print r asterisks for ($i=1; $i<= $r; $i++) { print (" *");} # drop cursor to a new line print "\n"; } # end for - unxsup:/home/bruce$ perl triangle.pl 5 * * * * * * * * * * * * * * * Cheer Bruce -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Help can't figure this one out
Bruce Ambraal wrote: > > I have written following coding to produce a triangle pattern(see below); > I now want to produce following paterns I can't figer this out > > (a)(b) (c) (d) diamond >** * * * * * * * * * * > * * * * * ** * * ** * * >* * * * * ** * * * * * * > * * * * * ** * * * * * * >* * * * * * ** * * * > * * * > * > __ > #!/usr/local/bin/perl -w > my $num_rows; > my $i; > my $r; > > $num_rows = ; > > for ($r = 1; $r <= $num_rows; $r++) > { > # indent by printing num_rows - r spaces > for ($i = 1; $i <= $num_rows - $r; $i++) {print (" \n");} > # print r asterisks >for ($i=1; $i<= $r; $i++) { print (" *");} > # drop cursor to a new line > print "\n"; > } > # end for #!/usr/local/bin/perl -w use strict; my $num_rows = shift || die "usage: $0 rows\n"; print "a)\n"; print ' ' x (($num_rows+1) - $_), ' *' x $_, "\n" for 1 .. $num_rows; print "\n"; print "b)\n"; print ' ', ' *' x $_, "\n" for reverse 1 .. $num_rows; print "\n"; print "c)\n"; print ' ' x (($num_rows+1) - $_), ' *' x $_, "\n" for reverse 1 .. $num_rows; print "\n"; print "d)\n"; print ' ' x (($num_rows+1) - $_), ' *' x (2 * $_ - 1), "\n" for 1 .. ($num_rows/2+1), reverse 1 .. ($num_rows/2); print "\n"; __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Help can't figure this one out
> -Original Message- > From: Bruce Ambraal [mailto:[EMAIL PROTECTED]] > Sent: Thursday, February 14, 2002 1:24 AM > To: [EMAIL PROTECTED]; [EMAIL PROTECTED] > Subject: Help can't figure this one out > > > I have written following coding to produce a triangle > pattern(see below); > I now want to produce following paterns I can't figer this out > > ... > > # indent by printing num_rows - r spaces > for ($i = 1; $i <= $num_rows - $r; $i++) {print (" \n");} A suggestion: if you want to write Perl programs, you should resist the temptation to write them like C programs. This kind of thing can be simplified to something like: print " \n" for 1 .. ($num_rows - $r); or better yet: print " \n" x ($num_rows - $r); If you haven't done so already, grab a copy of Schwartz' Learning Perl and work through it. That will show you some of the idioms that can greatly simplify your programs. (BTW, are you aware that this statement does not do what the comment says it does. You should fix one or the other.) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Help can't figure this one out
Just because you CAN take shortcuts in Perl doesn't necessarily mean that you should. Even though it's less typing, it might be more difficult to understand what's going on to someone else reading your code, or to yourself after looking at the code some time later. That's what I always believed, but that's just me. On Thursday 14 February 2002 05:47 am, you wrote: > > -Original Message- > > From: Bruce Ambraal [mailto:[EMAIL PROTECTED]] > > Sent: Thursday, February 14, 2002 1:24 AM > > To: [EMAIL PROTECTED]; [EMAIL PROTECTED] > > Subject: Help can't figure this one out > > > > > > I have written following coding to produce a triangle > > pattern(see below); > > I now want to produce following paterns I can't figer this out > > > > ... > > > > # indent by printing num_rows - r spaces > > for ($i = 1; $i <= $num_rows - $r; $i++) {print (" \n");} > > A suggestion: if you want to write Perl programs, you should > resist the temptation to write them like C programs. This kind > of thing can be simplified to something like: > >print " \n" for 1 .. ($num_rows - $r); > > or better yet: > >print " \n" x ($num_rows - $r); > > If you haven't done so already, grab a copy of Schwartz' > Learning Perl and work through it. That will show you some > of the idioms that can greatly simplify your programs. > > (BTW, are you aware that this statement does not do what the > comment says it does. You should fix one or the other.) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]