Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Merging Multiple Array elements

Reply
Thread Tools

Merging Multiple Array elements

 
 
Pradeep Patra
Guest
Posts: n/a
 
      01-15-2012
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;


@array = (@array1,@array2,@array3,@array4);

print "Array elements:@array";

It works and displays 1-12. But I need something different because i
dont know the value of "n" beforehand. So I decided to use a for loop.
I tried push but it did not work. I would appreciate any help to merge
multiple array elements preferably not using any library function like
push etc.


for (my $i = 1; $i < 3; $i++)
{
@array =(@array1,@array."$i+1"); ---> To add array1 to array 4 and
return final array
}


Regards
Pradeep
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      01-15-2012
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;
>
>
> @array = (@array1,@array2,@array3,@array4);
>
>print "Array elements:@array";
>
>It works and displays 1-12. But I need something different because i
>dont know the value of "n" beforehand.


Yes, you do know. Or how did you declare and define all those arrays
without knowing how many there are?

However, that is not the real the issue here. Whenever you find yourself
numbering your variables like above and on top of it you don't even know
how many variables you need, then you should seriously look into a
different data structure. In this case use an array, Luke! Or what do
you think those numbers are for?

>So I decided to use a for loop.


And then the for loop will work perfectly fine.

jue
 
Reply With Quote
 
 
 
 
Pradeep Patra
Guest
Posts: n/a
 
      01-15-2012

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


 
Reply With Quote
 
Wolf Behrenhoff
Guest
Posts: n/a
 
      01-15-2012
Am 15.01.2012 18:20, schrieb Pradeep Patra:
> It works and displays 1-12. But I need something different because i
> dont know the value of "n" beforehand. So I decided to use a for loop.
> I tried push but it did not work. I would appreciate any help to merge
> multiple array elements preferably not using any library function like
> push etc.
>
>
> for (my $i = 1; $i < 3; $i++)
> {
> @array =(@array1,@array."$i+1"); ---> To add array1 to array 4 and
> return final array
> }


This looks like you want to construct the name of a variable in your
program which is usually a bad idea: read perldoc -q "variable name"

The misconception here is that you have 4 separate arrays which belong
together in some way. So you could put (references to) all the arrays
into another array (or into a hash).

So:
my @x = ( [1,2,3], [4,5,6], [7,8,9], [10,11,12] );
my @merged = map @$_, @x;

- Wolf
 
Reply With Quote
 
Pradeep Patra
Guest
Posts: n/a
 
      01-15-2012
On Jan 15, 11:48*pm, Wolf Behrenhoff
<NoSpamPleaseButThisIsVal...@gmx.net> wrote:
> Am 15.01.2012 18:20, schrieb Pradeep Patra:
>
> > It works and displays 1-12. But I need something different because i
> > dont know the value of "n" beforehand. So I decided to use a for loop.
> > I tried push but it did not work. I would appreciate any help to merge
> > multiple array elements preferably not using any library function like
> > push etc.

>
> > for (my $i = 1; $i < 3; $i++)
> > {
> > * @array =(@array1,@array."$i+1"); *---> To add array1 to array 4and
> > return final array
> > }

>
> This looks like you want to construct the name of a variable in your
> program which is usually a bad idea: read perldoc -q "variable name"
>
> The misconception here is that you have 4 separate arrays which belong
> together in some way. So you could put (references to) all the arrays
> into another array (or into a hash).
>
> So:
> my @x = ( [1,2,3], [4,5,6], [7,8,9], [10,11,12] );
> my @merged = map @$_, @x;
>
> - Wolf


Thanks all for the replies.
My 4 separate arrays are returned from a method iterating as follows:

@array1= fun(i=1);
@array2= fun(i=2); and so on. I cannot change the func. So I need to
sum array elements merging the 4 arrays.

Any ideas/Suggestions? Sample code will help.

 
Reply With Quote
 
Pradeep Patra
Guest
Posts: n/a
 
      01-15-2012
Thanks Henry.I am trying to write code as per your suggestion. But how
will I get the below(my @arrays) initially from the func(). If you
could give some clue i will try to write code.

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

On Jan 16, 12:07*am, Henry Law <n...@lawshouse.org> wrote:
> On 15/01/12 18:58, Pradeep Patra wrote:
>
>
>
> > Any ideas/Suggestions? Sample code will help.

>
> No, _you_ write the code this time. *Your turn.
>
> --
>
> Henry Law * * * * * *Manchester, England


 
Reply With Quote
 
Wolf Behrenhoff
Guest
Posts: n/a
 
      01-15-2012
Am 15.01.2012 20:17, schrieb Pradeep Patra:
> Thanks Henry.I am trying to write code as per your suggestion. But how
> will I get the below(my @arrays) initially from the func(). If you
> could give some clue i will try to write code.
>
> my @arrays = ( [1,2,3], [4,5,6], [7,8,9], [10,11,12] );


Maybe you could read
perldoc perllol
section "Growing Your Own"

- Wolf
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      01-15-2012
Pradeep Patra wrote:
) Thanks all for the replies.
) My 4 separate arrays are returned from a method iterating as follows:
)
) @array1= fun(i=1);
) @array2= fun(i=2); and so on. I cannot change the func. So I need to
) sum array elements merging the 4 arrays.
)
) Any ideas/Suggestions? Sample code will help.

How about you give a clearer description of what exactly this func that you
cannot change does? What you wrote above is really awful. That either
means that that function is really awful, or that you dodn't describe it
properly.

Are you calling the method multiple times, once for each array?
Or does the function 'return' multiple arrays? (If so, how?)


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Pradeep Patra
Guest
Posts: n/a
 
      01-15-2012
On Jan 16, 12:32*am, Wolf Behrenhoff
<NoSpamPleaseButThisIsVal...@gmx.net> wrote:
> Am 15.01.2012 20:17, schrieb Pradeep Patra:
>
> > Thanks Henry.I am trying to write code as per your suggestion. But how
> > will I get the below(my @arrays) initially from the func(). If you
> > could give some clue i will try to write code.

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

>
> Maybe you could read
> perldoc perllol
> section "Growing Your Own"
>
> - Wolf



Thanks Wolf... Its awesome... Let me try if that works.
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      01-15-2012
Pradeep Patra wrote:
)
) 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?

Yes. Append the result of each function call directly to the whole array:

my @array;
for ( ... ) {
push @array, func1( ... );
}


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
merging multiple static library frakie C Programming 2 09-10-2007 07:47 AM
Merging children from different elements PC Paul XML 3 07-24-2007 07:06 AM
pairwise comparison and merging of vector elements Alan C++ 1 06-04-2007 06:12 PM
XML elements to JavaScript Array elements Conversion P XML 1 07-07-2006 09:08 PM
Merging Multiple Computers emails onto 1 computer Bill Lancaster Computer Support 3 03-19-2006 03:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57