in reply to Unique Character Combinations

Your solution is perhaps more elegant, but it's also less efficient, as data has to be passed back between instances of the function. I tested, and on the call:
my @arr = comb('A'..'R');
my code took approx. 23 seconds, while yours took 30.

Replies are listed 'Best First'.
Re^2: Unique Character Combinations
by polettix (Vicar) on May 02, 2005 at 13:52 UTC
    But you can work around this passing a reference to the final array:
    use strict; use warnings; print join "\n", comb_thormod('A'..'R'); sub comb_thormod { my $c_out = []; permute_thormod( '', $_, $c_out, @_ ) for ( 0 .. $#_ ); return @$c_out; } sub permute_thormod { my ( $str, $depth, $c_out, @chars ) = @_; if ( !$depth-- ) { foreach (@chars) { push @$c_out, $str . $_; } } else { permute_thormod( $str . $chars[$_], $depth, $c_out, @chars[ ( $_ + 1 ) .. ($#chars) ] ) for ( 0 .. $#chars ); } }
    even if results show your solution is still slightly more efficient (-2% / 5% scissor). OTOH, I'd still prefer the version without the static variable - should work in a multithreaded environment, at least :)

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.