|Perl: the Markov chain saw|
Re: HOW CAN WE MAKE A TRIANGLE LIKE THIS IN PERLby roboticus (Chancellor)
|on Mar 26, 2013 at 11:00 UTC||Need Help??|
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.
In the second one, we convert the C-style for loop into a simpler to read perl-style loop:
In perl, you can also modify a statement with if or for statement. Here we're using a for modifier on the print statement:
Then, of course, we can take advantage of some other interesting bits of the perl language. The 'x' operator, for instance:
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:
When your only tool is a hammer, all problems look like your thumb.