Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate permutations from this array?

Tags:

arrays

perl

I have an array

my @arr1 = ("A1", "B1,B2", "C1,C2,C3", ..); 

how to create new array from this by appending the elements in such a way that it will take one sub-element from @arr1 like A1, B1 & C1 to create a new element "A1B1C1". Sub-elements are separated by ",". The array element number is dynamic.

So the resulting array should look as

@out = qw/A1B1C1 A1B1C2 A1B1C3 A1B2C1 A1B2C2 A1B2C3/; 
like image 832
waghso Avatar asked Feb 16 '26 14:02

waghso


1 Answers

You could perform dynamic recursion or reach out to a CPAN module, but I'd use glob in this case:

my @out = glob join '', map "{$_}", @arr1;
like image 190
Zaid Avatar answered Feb 19 '26 04:02

Zaid