#!/usr/bin/perl -wl use strict; use Data::Dumper; my $N = 4; my $size = 32; my $arr = [[1, 2], [3, 4], [5, 6], [7, 8]]; my @C; foreach my $m (0..2**$N - 1) { my $vec = ""; vec($vec, 0, $size) = $m; my @bits = split(//, unpack("B*", $vec)); splice(@bits, 0, $size - $N); # so @bits now contains the binary expansion of $m # with exactly $N bits, as you can verify by uncommenting # print join "::", @bits; $C[ $m ] = 1; # now we multiply the $N terms $arr->[i][j] # where j is the i-th bit in the binary expansion of $m $C[ $m ] *= $arr->[ $_ ][ $bits[$_] ] for 0..$N-1; } print Dumper \@C;