The reason I need this requirement because I have something as
follows:
@array1 = returned by func1(i=0); [NOTE: this is just for illustration
not actual code]
@array2 = returned by func1(i=1);
I cannot change the func1(i); So I have to summing all the sub arrays
returned by func1(i) itself.
Any suggestions how to go about this?
On Jan 15, 11:10*pm, Henry Law <n...@lawshouse.org> wrote:
> On 15/01/12 17:20, Pradeep Patra wrote:
>
>
>
> > Hi all,
> > * * I want to merge more than 2 arrays.
>
> > For example:
>
> > @array1 = (1,2,3);
> > @array2 = (4,5,6);
> > @array3 = (7,8,9);
> > @array4 = (10,11,12);
> > my @array;
>
> > for (my $i = 1; $i< *3; $i++)
> > {
> > * *@array =(@array1,@array."$i+1"); *---> *To add array1 to array 4 and
> > return final array
> > }
>
> The code you posted doesn't even come near to doing what you want: had
> you actually tried it?
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @array1 = (1,2,3);
> my @array2 = (4,5,6);
> my @array3 = (7,8,9);
> my @array4 = (10,11,12);
> my @array;
>
> for (my $i = 1; $i < 3; $i++)
> {
> * *@array =(@array1,@array."$i+1");
>
> }
>
> print "result is: ", (join ',',@array), "\n";
>
> ./tryout
> result is: 1,2,3,42+1
>
> While you call your arrays by fixed names (array1, array2, arrayfoo)
> you're not going to be able to do this easily. *I suggest you use an
> array of arrays, over which you can then iterate, merging the contents
> of each sub-array as you go. *This code does what you want but I make no
> assertions as to its elegance.
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @arrays = ( [1,2,3], [4,5,6], [7,8,9], [10,11,12] );
> my @merged;
> for ( @arrays ) {
> * *@merged = ( @merged, @$_ );}
>
> print "Result is ", (join ',', @merged), "\n";
>
> ./tryout
> Result is 1,2,3,4,5,6,7,8,9,10,11,12
>
> --
>
> Henry Law * * * * * *Manchester, England