Hi!
If you use PDL (Perl Data Language
http://pdl.perl.org/), you can collapse dimensions of piddles using the function "clump". This will allow you to do what you want in amazingly few steps:
$a = pdl @your_array;
$b = clump($a,2); # collapses the first two dimensions
print $b;
Hope this helps!