in reply to How to flatten an x-dimensional array?
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'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How to flatten an x-dimensional array?
by demerphq (Chancellor) on Mar 12, 2002 at 14:26 UTC |