Perl: What is the easiest way to flatten a multidimensional array?

sid_com picture sid_com · Mar 2, 2011 · Viewed 10.3k times · Source

What's the easiest way to flatten a multidimensional array ?

Answer

Vijayender picture Vijayender · Dec 4, 2012

One level of flattening using map

$ref = [[1,2,3,4],[5,6,7,8]]; # AoA

@a = map {@$_} @$ref;         # flattens it

print "@a";                   # 1 2 3 4 5 6 7 8