Thanks for the advice. Forgive me if I sounded like someone who's frustrated, 
couldn't do his homework asking somebody else for help.

When I was learning C programming, I read that learning those difficult 
algorithms such as bubble sort, quick sort, binary search, is something that 
only programming students have to deal with. 

I have also learned that the best way to learn is by looking at other peoples 
code. So far I managed to write a number of useful perl programs through this. 
I just got frustrated with this particular task I had to accomplish. I've done 
some readings before about those math topics you've mentioned. As I understood, 
permutation is producing all combinations without repetition. A part of my code 
uses the module Algorithm::Permute for this purpose and for the other part 
which produces all possible (repeating) combinations, I have to write this 
code. Somebody gave me an example pseudocode and I tried to convert my code 
into it but I just couldn't do it.

I knew I needed a recursive function, I just didn't know how to start.
What confused me more is that the code I found is a lot different from what I 
was initially thinking. He used the builtin function substr in the code in a 
way that I couldn't figure how it worked.

See below:

my $result = '';

perm(0,2);

sub perm($$){
my ($cur,$max) = @_;

if ($cur>=$max){
 print "$result\n";
return;
}

for(@word){
substr($result,$cur,1)=$_;
perm($cur+1,$max);
}
}

What's the use of double ($$) after sub perm?? Does it have anything to do with 
process IDs (perldoc perlvar says so)?

This line is also confusing:
substr($result,$cur,1)= $_;
 
Substr returns a list, right? The first parameter is the expression, 2nd is the 
offset, and last is the length.
The above line is too cryptic for me. What confused me more is that the return 
value of substr was assigned the $_.


If someone can help me decipher each line, i'll be very happy.



--- On Mon, 10/26/09, Thomas Bätzler <t.baetz...@bringe.com> wrote:

> From: Thomas Bätzler <t.baetz...@bringe.com>
> Subject: AW: compact my wordlist generator
> To: "begginers perl.org" <beginners@perl.org>
> Cc: "Michael Alipio" <daem0n...@yahoo.com>
> Date: Monday, October 26, 2009, 10:40 PM
> 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