in reply to Iterating through string combinations
If you can't get rid of the recursion, you could always tighten up the program. If you create a mapping of characters to their next character, look how short your final subroutine becomes:
#!/usr/bin/perl -w use strict; my $number = '000'; my %mapping; @mapping{ 0..9,'a'..'z' } = ( 1..9,'a'..'z','0' ); do { increment( \$number ); print "$number\n"; } until $number eq 'zzzzzzzzzzzzzzzz'; # or some other long string sub increment { my ( $number_ref ) = @_; my @digits = split //, $$number_ref; unshift @digits, undef while @digits < 16; _increment_digit( \@digits, @digits - 1 ); $$number_ref = join '', grep { defined } @digits; } sub _increment_digit { my ( $array_ref, $index ) = @_; $array_ref->[$index] ||= 0; $array_ref->[$index] = $mapping{ $array_ref->[$index] }; _increment_digit( $array_ref, ($index - 1) ) if $array_ref->[$inde +x] eq 'z'; }
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (Ovid) Re: Iterating through string combinations
by atcroft (Abbot) on Dec 18, 2001 at 05:13 UTC |