bowsayge said:
Susanne said to us:
[...]
but this way i only got [a, aa, ab, ac, ad, ae....] but nothing over two
letters.
[...]
Is something like this what you want to do?
#!/usr/bin/perl
use strict;
use warnings;
my ($length, @test) = (4, 'a'..'c');
sub func {
my ($bref, $depth) = (@_);
if ($depth >= $length) {
print "$bref\n";
return;
}
foreach my $var (@test) {
func("$bref$var", $depth + 1);
}
}
func '', 0;
The length of the string is always 4 chars with this program.
Bowsayge doesn't know how to include the shorter strings such
as "aa, ab, ... abc". Perhaps someone can modify the program
to get those too?
Very easy. Just call the function with different values in the
global $length:
for ( 1 .. 4 ) (
$length = $_; # loop var would have been aliased
func '', 0;
}
But the length and also the alphabet (@test) should really be
function parameters, not globals.
sub func {
my ($bref, $depth, $length, @alphabet) = (@_);
if ($depth >= $length) {
print "$bref\n";
return;
}
foreach my $var (@alphabet ) {
func("$bref$var", $depth + 1, $length, @alphabet);
}
}
func( '', 0, $_, 'a' .. 'c') for 1 .. 4;
This still isn't very pretty. The first two parameters are only
needed to start the recursion off, the user (of func()) shouldn't
have to deal with them. The standard solution is to make func() an
internal routine "_func_recurs" (say) and give the user (untested):
sub func {
my ( $length, @alphabet) = @_;
_func_recurs( '', 0, $length, @alphabet);
}
There may be better ways to integrate the call.
Anno