in reply to Re^2: Generating combinatorial strings
in thread Generating combinatorial strings
Ah, madd, I appreciate when people answer me directly, if possible, without complaining that I've phrased my question poorly. So, I'm slow to criticize your question. But after some thought, I'm not entirely sure you're well served by the answers you're getting (or would have gotten from me). I'd like to talk about your question a little.
Your question appears to be very simple -- the sort of thing that is posed in the first week of a freshman programming class. Here's a freshman answer:
#!/usr/bin/perl use strict; use warnings; for my $A (1..3) { for my $B (1..2) { for my $C (1..3) { print "$A$B$C\n"; }; }; }; __END__ = for output 111 112 113 121 122 123 211 212 213 221 222 223 311 312 313 321 322 323 =cut
Looking over the very interesting (to me) replies your post got, it seems we all thought instantly of the freshman answer and immediately rejected it. That's what I did. The freshman answer is Not Good in a technical sense; it's offensive to professionals, even experienced amateurs. Each of us has a set of objections to it: The freshman answer is too brittle, too silly, too difficult to maintain, insufficiently general. Therefore, each reply seems to address one or more of these flaws.
Maybe the primary reason that the freshman answer is so offensive is that it uses nested for loops. There are various good reasons to avoid these but I'd like to note that all the solutions presented do use nested loops; in a problem of your kind they're nearly unavoidable. What these solutions do is hide the looping, often within a library module. That's often a good way to avoid Bad Code but I'm not sure it helps you.Perhaps I overreach myself to explain my betters' motives; but I can certainly speak for myself. On reading your post, I immediately started to build a Very Clever solution that would have given you a choice between entering parameters on the command line or while running interactively, at a series of prompts; several choices of formatting and output methods; and a clever little recursive subroutine at the heart of it all. Then I stopped and asked myself Why? I don't even know what you really want, or need.
To solve a person's problem, we need to find out more about that person. Often the technical issues are trivial. The only thing apparent to me is that you are a chemist, or work in that field. Like me, you're a PerlMonks novice; unlike me, perhaps, you may have very little programming experience. Maybe the freshman solution is just what you want. At the very least, it's easy to understand. I just don't know; and I'm not sure anyone else does, either.
I absolutely do not want to make you feel awkward; I feel awkward myself, often, when asking for help. I'm going to return to this node and I'll do my best to help (provided others don't do or haven't done much better than I can). But I'd appreciate knowing more about why you want this problem solved; how you intend to apply it; in what setting (school, industry, lab) the question arose. What will make the solution work for you?
|
|---|