Concatenating arrays from arrays of arrays

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.

I have been programming in Unicon which has a concatenate operator, but Perl
makes very heavy weather of nested list management. Should I switch to Lisp,
or is there a way of doing what I want to do in Perl?

Regards

_John Sampson_
0
sampson
3/15/2008 10:14:32 PM
perl.beginners 29360 articles. 3 followers. Follow

4 Replies
787 Views

Similar Articles

[PageSpeed] 15

On Sat, Mar 15, 2008 at 6:14 PM, John Sampson
<sampson.john7@googlemail.com> wrote:
> 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.
>
>  I have been programming in Unicon which has a concatenate operator, but Perl
>  makes very heavy weather of nested list management. Should I switch to Lisp,
>  or is there a way of doing what I want to do in Perl?
>
>  Regards
>
>  _John Sampson_
snip

Um, I am uncertain what you are trying to do, can you provide the data
you are reading in and what form you want it to have?  It sounds like
you want the push function:

my @array;
while (<>) {
    push @array, split;
}


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
0
chas
3/15/2008 10:24:07 PM
John Sampson wrote:
> 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.

Is this what you are talking about?

     my @arr1 = ( [ 1, 2 ], [ 3, 4 ] );
     my @arr2 = ( [ 5, 6 ], [ 7, 8 ] );
     my @newarray;
     foreach ( @arr1, @arr2 ) {
         push @newarray, @$_;
     }
     print "@newarray\n";

If not, please translate the above into Perl by posting your best code 
attempt.

> The data is to be read in from a file rather than existing as 
> literals in my code.

 From a file? In that case, why bother with the arrays of arrays?

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

Let's see it. And please include sample data.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
0
noreply
3/15/2008 10:41:13 PM
John Sampson wrote:
> 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.
> 
> I have been programming in Unicon which has a concatenate operator, but Perl
> makes very heavy weather of nested list management. Should I switch to Lisp,
> or is there a way of doing what I want to do in Perl?

I'm intrigued to know what makes you think Perl is poor at list
management.

   push @a, @b;

will concatenate the data in @b onto the end of @a if that's what you
want.

But it sounds very much like you've decided to read data from a file
into an array of arrays and then take it out again. It's likely to be
better just to read the data in and store it in its final form straight
away.

It would help if you'd given some sample data, but the program below
accumulates all the lists in @aofa into array @flat.

HTH,

Rob



use strict;
use warnings;

my @aofa = (
   [1,2,3,4],
   [5,6,7,8],
   [9,10,11,12],
);

my @flat;

push @flat, @$_ foreach @aofa;

print "@flat\n";
0
rob
3/15/2008 10:41:42 PM
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 \@flat;
'
$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
0
krahnj
3/16/2008 1:10:25 AM
Reply: