wagh wagh - 4 months ago 9
Perl Question

perl append array elements together & create new array

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/;

Answer

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;