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.
In reply to (Ovid) Re: Iterating through string combinations
by Ovid
in thread Iterating through string combinations
by patgas
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |