in reply to combinations of given string
You also need a recursive solution that creates all combinations directly without the need of filtering duplicates:
use strict; use warnings; my $input = 'ABC---'; sub insert { my( $sofar, $chars, $dashes ) = @_; print "$sofar\n" and return unless @$chars or @$dashes; insert( $sofar . $$chars[0], [ @$chars[ 1..@$chars-1] ], [ @$dash +es ] ) if @$chars; insert( $sofar . $$dashes[0], [ @$chars ], [ @$dashes[ 1..@$dashes-1 + ] ] ) if @$dashes; } insert( '', [ $input =~ /[^-]/g ], [ $input =~ /-/g ] );
UPDATE: operating on strings rather than arrays is even simpler:
use strict; use warnings; my $input = 'ABC---'; sub insert { my( $sofar, $chars, $dashes ) = @_; print "$sofar\n" and return unless $chars or $dashes; insert( $sofar . substr( $chars, 0, 1), substr( $chars, 1 ), $dashe +s ) if $chars; insert( $sofar . substr( $dashes, 0, 1), $chars, substr( $dashes, 1) + ) if $dashes; } insert( '', $1, $2 ) if $input =~ /^([^-]+)(-+)$/;
|
|---|