Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

HOW CAN WE MAKE A TRIANGLE LIKE THIS IN PERL

by kalyanbhave (Initiate)
on Mar 26, 2013 at 10:14 UTC ( #1025485=perlquestion: print w/ replies, xml ) Need Help??
kalyanbhave has asked for the wisdom of the Perl Monks concerning the following question:

I have used C code to print a equiateral triangle in this

**** *** ** *

clear [int] $n = Read-Host for ( $i = $n ; $i -gt 0 ; $i -- ) { for($j =0 ; $j -lt $i ; $j ++ ) { write-host "*" }}

Comment on HOW CAN WE MAKE A TRIANGLE LIKE THIS IN PERL
Select or Download Code
Re: HOW CAN WE MAKE A TRIANGLE LIKE THIS IN PERL
by daxim (Chaplain) on Mar 26, 2013 at 10:20 UTC
    my $size = 4; printf "%s\n", '*' x $_ for reverse 1..$size;
Re: HOW CAN WE MAKE A TRIANGLE LIKE THIS IN PERL
by Anonymous Monk on Mar 26, 2013 at 10:51 UTC
Re: HOW CAN WE MAKE A TRIANGLE LIKE THIS IN PERL
by hdb (Parson) on Mar 26, 2013 at 10:56 UTC
    $_ = "*****"; print $_,$/ while( s/\*// );
Re: HOW CAN WE MAKE A TRIANGLE LIKE THIS IN PERL
by roboticus (Canon) on Mar 26, 2013 at 11:00 UTC

    kalyanbhave:

    You should just try a direct translation, first (be sure to add strict and warnings), and it'll look pretty recognizable. Then, as you learn perl, you can add more tweaks. Here's a version of your code through one of many possible evolutionary branches.

    The first variation is a direct translation of your code--it's pretty darned similar in perl.

    $ cat t.pl #!/usr/bin/perl use strict; use warnings; my $n = <>; print "Direct translation\n"; for (my $i=$n; $i > 0; $i--) { for (my $j=0; $j < $i; $j++) { print "*"; } print "\n"; }

    In the second one, we convert the C-style for loop into a simpler to read perl-style loop:

    print "A bit more perlish: convert C-style for loops\n" . "into perl style for loops\n"; for my $i (reverse 1 .. $n) { for my $j (0 .. $i) { print "*"; } print "\n"; }

    In perl, you can also modify a statement with if or for statement. Here we're using a for modifier on the print statement:

    print "Yet more perlishiosness: use the for statement modifier\n"; for my $i (reverse 1 .. $n) { print "*" for 0 .. $i;; print "\n"; }

    Then, of course, we can take advantage of some other interesting bits of the perl language. The 'x' operator, for instance:

    print "Not enough perlbell: use 'x' operator for inner loop\n"; for my $i (reverse 1 .. $n) { print "*" x $i, "\n"; }

    A bigger jump: If you think about your program, what you're really doing is just transforming a list of integers into lists of strings. The map operator takes a list and allows you to perform a translation on each item, giving you a new list. So we'll take each integer and convert it to a string of stars followed by a newline:

    print "Jumping the shark: adding some per(i)lousness\n"; print map { "*" x $_, "\n" } reverse 1 .. $n;

    I advise you to play around, read perldoc perlop and perldoc perlfunc to find out how perl likes to do things, and play around with it.

    Have fun!

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: HOW CAN WE MAKE A TRIANGLE LIKE THIS IN PERL
by kcott (Abbot) on Mar 26, 2013 at 14:55 UTC

    G'day kalyanbhave,

    Here's another way to do it:

    $ perl -E 'say "*" x $_ for reverse 1 .. <>' 4 **** *** ** *

    -- Ken

      I see Tiger Woods is not the only one playing good Golf this week.... :o)
      A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: HOW CAN WE MAKE A TRIANGLE LIKE THIS IN PERL
by GrandFather (Cardinal) on Mar 27, 2013 at 03:29 UTC

    An "Xmas tree" may be more satisfying:

    my $n = <>; print ' ' x $n, "+\n"; print ' ' x ($n - $_), '*' x (1 + 2 * $_), "\n" for 1 .. $n - 1;
    True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025485]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2014-08-20 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (113 votes), past polls