John Sampson wrote:
Hello -

Hello,

I am trying to accumulate items in a flat list (array) by
concatenating on to it
the scalars contained in arrays which in turn are contained in arrays. The data
is to be read in from a file rather than existing as literals in my code.

Everything I try either crashes the computer or gives me an array of
arrays instead
of a flat list.

It sounds like you need something like this:

$ perl -le'
use Data::Dumper;
my @array = ( 1, 2, 3, [ 4, 5, 6, [ 7, 8, 9 ] ], [ 10, 11, 12, [ 13, 14, 15 ] ], 16, 17, 18 );
sub flatten { map ref eq q[ARRAY] ? flatten( @$_ ) : $_, @_ }
my @flat = flatten @array;
print Dumper [EMAIL PROTECTED];
'
$VAR1 = [
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9,
          10,
          11,
          12,
          13,
          14,
          15,
          16,
          17,
          18
        ];



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to