Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: Generate sequential array element combos

by Anonymous Monk
on Jul 28, 2004 at 04:57 UTC ( [id://377924]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Generate sequential array element combos
in thread Generate sequential array element combos

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.
  • Comment on Re^3: Generate sequential array element combos

Replies are listed 'Best First'.
Re^4: Generate sequential array element combos (.. and ++)
by tye (Sage) on Jul 28, 2004 at 05:37 UTC

    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        

Re^4: Generate sequential array element combos
by BrowserUk (Patriarch) on Jul 28, 2004 at 06:22 UTC

    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
Re^4: Generate sequential array element combos
by hbo (Monk) on Jul 28, 2004 at 05:53 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://377924]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 18:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found