my @array = ( [ '', '' ], [ '', '' ], [ 'funct1', '', '' ], [ '', '' ], [ '', [ '', [ 'funct2a', 'funct2b', '' ], '' ], '' ], 'funct3', 'funct4', 'funct5', 'funct6', 'funct7', ); print join "\n", _flatten( @array ), "\n"; { my @results; sub _flatten { foreach (@_) { if (ref $_ eq 'ARRAY') { _flatten( @{ $_ } ); next; } push @results, $_ if $_; } return @results; } }
If however, this code is to be used in a production environment, I would consider either rewriting the above to include depth checking and/or a shift towards a iterative rather than recursive loop, or alternatively, reevaluate the code generating this complex data structure in the first place.
perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'
In reply to Re: How to flatten an x-dimensional array?
by rob_au
in thread How to flatten an x-dimensional array?
by Incognito
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |