sid_com sid_com - 6 months ago 23
Perl Question

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

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

Answer

Using List::Flatten seems like the easiest:

use List::Flatten;

my @foo = (1, 2, [3, 4, 5], 6, [7, 8], 9);
# @foo contains 6 elements, 2 of them are array references

my @bar = flat @foo;
# @bar contains 9 elements, same as (1 .. 9)

Use List::Flatten::Recursive to do it recursively.