What\'s the easiest way to flatten a multidimensional array ?
Same as Vijayender's solution but will work on mixed arrays containing arrayrefs and scalars.
$ref = [[1,2,3,4],[5,6,7,8],9,10];
@a = map { ref $_ eq "ARRAY" ? @$_ : $_ } @$ref;
print "@a"
Of course you can extend it to also dereference hashrefs:
@a = map { ref $_ eq "ARRAY" ? @$_ : ref $_ eq "HASH" ? %$_: $_ } $@ref;
or use grep to weed out garbage:
@a = map { @$_} grep { ref $_ eq 'ARRAY' } @$ref;
As of List::MoreUtils 0.426 we have an arrayify function that flattens arrays recursively:
@a = (1, [[2], 3], 4, [5], 6, [7], 8, 9);
@l = arrayify @a; # returns 1, 2, 3, 4, 5, 6, 7, 8, 9
It was introduced earlier but was broken.