Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
886 views
in Technique[技术] by (71.8m points)

combinations - In Perl , How to generate All Possible Patterns ,

may i know how to ( in Perl ) , generate below All Possible Patterns in a file and on screen output , and each slot in the pattern can be accessed , ?!

many thanks for all ,

input value ,

1 , no. of slots ,

2 , no. of objects ,

for example ,

no. of object = 2 , { a , b } ,

no. of slots = 4 ,

then , output ,

no. of all possible patterns = 2^4 = 16 ,

then ,

row is 16 ,

column is 8 ,

eachSlot[i][j] = allow assign or change its value ,

then , output format look like ,

a a a a

a a a b

a a b a

a a b b

a b a a

a b a b

a b b a

a b b b

b a a a

b a a b

b a b a

b a b b

b b a a

b b a b

b b b a

b b b b

and ,

if see 'a' , then do sth actionX ,

if see 'b' , then do sth actionY ,

many thanks for all the advices and helps ,

question from:https://stackoverflow.com/questions/65949608/in-perl-how-to-generate-all-possible-patterns

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
use Algorithm::Loops qw( NestedLoops );

my @syms = qw( a b );
my $num_slots = 4;

my $iter = NestedLoops([ ( @syms ) x $num_slots ]);
while ( my @items = $iter->() ) {
   say "@items";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...