in reply to Generate sequential array element combos

And not only what BrowserUk says, but you might also want to explain what
my @array = (0..9); my $start = 4; my $end = 6;
has to do with it.

davidj

Replies are listed 'Best First'.
Re^2: Generate sequential array element combos
by exussum0 (Vicar) on Jul 28, 2004 at 03:08 UTC
    It LOOkS like, he wants to do all permutations ('cept backwards from my example) using 4 characters to 6. The counting scheme seems such that
    0000 0001 0011 0101 1001 0002 0012 0102 1002 0009 0019 0109 1009 0029 0209 2009

    Bart: God, Schmod. I want my monkey-man.

      I'm with you up to '1009', but after that it's guess work. The only clue we have is that the final number is all nines. That suggests something like what you present, but it could just as well jump from '9001' (using his digit order) to 00000, the first five digit entry.

      Insufficent data.

      "Even if you are on the right track, you'll get run over if you just sit there." - Will Rogers
      Yeah I guess that is kinda weird list I gave.

      You are right though. I want to step through every possible combination given the integers 0 through 9 from 4 to 6 digits long. I guess a better list would be:

      0000
      0001
      0002
      0003
      0004
      0005
      0006
      0007
      0008
      0009
      0010
      0011
      0012
      ....
      ....
      9999 # Once the script maxes each place digit at 9
      00000 # it adds another place digit and starts over.
      00001
      .....
      .....
      99999 # Same here when all 5 digits are maxed at 9 it
      000000 # adds a 6th digit and starts over.
      000001
      ......
      ......
      999999
      Hope that makes more since.

        For that case you can get as simple as

        for( map '0'x$_ .. '9'x$_, 4..6 ) {

        but that chews up quite a chunk of memory. You can use tons less memory with something like

        for my $len ( 4 .. 6 ) { my $pat = '0' x $len; while( $len == length $pat ) { # ... $pat++; } }

        But, of course, that won't help much if you hope to change your list of characters to something other than 0..9.

        Algorithm::Loops would probably be quite helpful for this if you do hope to use characters other than digits. But I think I'll not code an example for that at this point, though it can be done fairly simply (or more complexly if you want a single iterator, for example).

        - tye        

        Mostly springing from tye's solution, but it avoids the memory growth.

        $\ = "\n"; for $n ( 4 .. 6 ){ print for '0' x $n .. '9' x $n; }

        And if you needed different 'digits' then

        $\ = "\n"; my @digits = 'a' .. 'j'; for my $n ( 4 .. 6 ) { print @digits[ split '', $_ ] for '0' x $n .. '9' x $n; }

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
        This is a class assignment, isn't it?

        "Even if you are on the right track, you'll get run over if you just sit there." - Will Rogers