Michael Alipio <daem0n...@yahoo.com> wrote:
> Can anyone tell me how the code above works? My original program must
> deal with arbitrary length and generate all the possible combinations
> (even repeating) of a particular set. What could take me gazillions of
> for loops for that, somebody just came up with less than ten lines.
> I can just copy and paste the code I found above but I want to learn how
> it works. How could someone write this code so easily but when I tried
> even writing just a pseudocode for it, my head almost exploded.

It probably helps if you have some understanding of the underlying mathematics 
of the problem domain, i.e. in this case combinatorics.

This would also clear up potential misunderstandings viz the definition of a 
permutation ("randomly pick n of m elements without putting them back") vs. 
that of a combination ("randomly pick n of m elements while putting them back").

> I want to be a good programmer, but at this rate, I don't think I can be
> one if I will just keep copying and pasting someone else's code or
> downloading modules from CPAN.

Actually, taking somebody else's code and trying to figure out how it works is 
a good exercise ;-)
 
> Can anyone teach me how my own code (the one with gazillion for loops),
> can be converted into a pseudocode then eventually into a working sub
> procedure?

No, probably not right away. But the good news is that most people don't come 
up with elegant solutions off the top of their heads - instead, they will have 
learned a number of basic patterns and/or algorithms for solving common 
problems, and they will have the experience to adapt what they know to new 
problems.

So, in order to become a better programmer you should look at stuff people have 
been doing before you. Go to your local library and borrow a book on 
Algorithms. Try to implement them in Perl. Have a look at the Perl cookbook. If 
you want a challenge, then treat yourself to a copy of "Higher Order Perl" by 
Mark Jason Dominus.

HTH,
Thomas


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to