in reply to Generating combinatorial strings

So you have noticed that someone has already posted a nearly identical question. Why don't you use the answers given to that question then?. E.g Re: Generating lists of strings, which seems to fit nicely to your problem too...
Rata (just wondering)

Replies are listed 'Best First'.
Re^2: Generating combinatorial strings
by madd (Acolyte) on Jan 25, 2010 at 08:49 UTC

    I can indeed use these answers in *most* cases, however, all answers given to that question seem to assume that the maximum index is the same in all cases. I do not have the luxury of this assumption.

    Considering a simple case of three bonds. "a" and "c" may be rotated 3 times, "b" only twice. i.e.:

    a -> 3 b -> 2 c -> 3 which yields the strings 111 112 113 121 122 123 211 212 213 221 222 223 311 312 313 321 322 323

    That is to say, "3" is not a legal value in the second column. Or phrased another way, I need to do the same thing as anon, but set a (possibly different) limit on each digit making up the string.

      #! perl -slw use strict; sub nFor(&@) { my $code = shift; die "First argument must be a code ref" unless ref( $code ) eq 'CO +DE'; my @limits = @_; my @indices = ( 0 ) x @limits; for( my $i = $#limits; $i >= 0; ) { $i = $#limits; $code->( @indices ), ++$indices[ $i ] while $indices[ $i ] < $limits[ $i ]; $i = $#limits; $indices[ $i ] = 0, ++$indices[ --$i ] while $i >= 0 and $indices[ $i ] == $limits[ $i ]; } } my @digits = 1 .. 3; ## Set this to the maximum set nFor { my @numbers = @digits[ @_ ]; my $name = join'', @numbers; print $name; } 3, 2, 3; ## Set these to the individual limits. __END__ c:\test>nFor.pl 111 112 113 121 122 123 211 212 213 221 222 223 311 312 313 321 322 323

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      You might still use one of the algorithms proposed in the previous thread. However you need to add an additional check on validity (e.g. don't use the value if the second digit is a 3).
      HTH, Rata

      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:

      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?