Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Words generation algorithm

by blokhead (Monsignor)
on Nov 04, 2007 at 02:36 UTC ( #648835=note: print w/ replies, xml ) Need Help??

in reply to Words generation algorithm

A solution using tye's Algorithm::Loops:

use Algorithm::Loops 'NestedLoops'; my %pat = ( L => ['a' .. 'z'], N => [0 .. 9] ); my $pattern = shift || "LLN"; NestedLoops( [ @pat{ split //, $pattern } ], sub { print join("", @_), $/ } );
Or one using glob:
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);
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.

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?


Comment on Re: Words generation algorithm
Select or Download Code
Replies are listed 'Best First'.
Re^2: Words generation algorithm
by Gangabass (Vicar) on Nov 04, 2007 at 07:08 UTC

    It's work like the charm! Thanks!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://648835]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2015-11-29 11:50 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (750 votes), past polls