sub iterate_dim{ [map[map$_*2-1,split"",$_],map{substr unpack("B*",chr$_-1),-$_[0]}1..2**$_[0]] } #and call it: my $AoA = iterate_dim( 2 );