I want to do permutation in Perl. For example I have three arrays: ["big", "tiny", "small"]
and then I have ["red", "yellow", "green"]
and also ["apple", "pear", "banana"]
.
How do I get:
["big", "red", "apple"] ["big", "red", "pear"] ..etc.. ["small", "green", "banana"]
I understand this is called permutation. But I am not sure how to do it. Also I don't know how many arrays I can have. There may be three or four, so I don't want to do nested loop.
IF
then you can simply do this:
For two arrays @xs
and @ys
:
map{ my $x = $_; map { [$x, $_] } @ys } @xs
For three arrays @xs
, @ys
, @zs
map{ my $x = $_; map { my $y = $_; map { [$x, $y, $_] } @zs } @ys } @xs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With