Or one using glob:use Algorithm::Loops 'NestedLoops'; my %pat = ( L => ['a' .. 'z'], N => [0 .. 9] ); my $pattern = shift || "LLN"; NestedLoops( [ @pat{ split //, $pattern } ], sub { print join("", @_), $/ } );
However, the glob version will generate all the expansions in memory (even if you use the iterator interface to glob). NestedLoops generates them as needed.my %pat = ( L => ['a' .. 'z'], N => [0 .. 9] ); my $pattern = shift || "LLN"; $_ = sprintf "{%s}", join ",", @$_ for values %pat; my $glob = join "", @pat{ split //, $pattern }; print "$_\n" for glob($glob);
BTW, if your pattern calls for all 10-character-long strings, you will have between 10 billion and 100,000 billion strings generated. I'm guessing you probably don't need them all. What do you really want to accomplish?
blokhead
In reply to Re: Words generation algorithm
by blokhead
in thread Words generation algorithm
by Gangabass
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |