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?


In reply to Re^3: Generating combinatorial strings by Xiong
in thread Generating combinatorial strings by madd

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.