Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.


In reply to Re: HOW CAN WE MAKE A TRIANGLE LIKE THIS IN PERL by roboticus
in thread HOW CAN WE MAKE A TRIANGLE LIKE THIS IN PERL by kalyanbhave

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (10)
    As of 2014-04-23 09:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (541 votes), past polls