in reply to Getting combinations from a number's digits

Most programmers would differentiate between single variables and arrays. If what you want is to store those values in an array then you could do it like this:

my @array = (123,132,213,213,312,312);

However that seems a pretty trivial thing to want to do. I suspect this is an XY Problem. How about asking the real question that is bothering you rather than a question pertaining to what you think might be the solution. Showing some code that you have tried would help a lot too.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Getting combinations from a number's digits
by PerceptiveJohn (Acolyte) on Jul 10, 2006 at 00:35 UTC
    I'll try again: I,m accepting a 3 digit number from a form.

    That number has 6 possible 3-digit combinations. I have to know what those 6 3-digit combinations are so that I can take each one and check to see if it's in a MySql data base table and, if so, tell the user what dates each of the 6 combinations appeared.

    It's a lottery table look-up.

    Does this help? We cuurently work in a MicroFocus Cobol environment which allows us to index each digit and manipulate them.

      Ok, that's a better question. Still no code, but maybe this will get you started:

      use warnings; use strict; use Algorithm::Loops qw(NextPermute); my $number = '123'; my @digits = split //, $number; my @array = ($number); # Add the first permutation push @array, join '', @digits while NextPermute (@digits); print "@array";

      Prints:

      123 132 213 231 312 321

      DWIM is Perl's answer to Gödel
      If you have access to change the database, I wonder whether it would be better to store all the numbers sorted (either in the main table, or in a separate table), and then look them up that way. So if the user enters "312", you'd check for "123". Then you're only doing one lookup instead of 6.