Merging Multiple Array elements

P

Pradeep Patra

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:mad: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
 
J

Jürgen Exner

Pradeep Patra said:
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:mad: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
 
P

Pradeep Patra

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?


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
 
W

Wolf Behrenhoff

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
 
P

Pradeep Patra

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.
 
P

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] );
 
W

Wolf Behrenhoff

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
 
W

Willem

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
 
P

Pradeep Patra

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.
 
W

Willem

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
 
J

Jürgen Exner

Pradeep Patra said:
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, several:
- If you really want to collect the individual return results before
summing them up then use an array of (references to) arrays as explained
before.
- with each iteration (i=0, i=1, ...) don't store the return result, but
add it to your grand total right away
- and last but not least: please post actual Perl code, not some
pseudo-code

jue
 
J

Jürgen Exner

Pradeep Patra said:
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.

Somehow you are not telling the true story. Do you have 4 distinct
function calls and 4 distinct arrays? Or don't you know how many
function calls there are therefore how many arrays there are?
It is either the one or the other, it can't be both.
If it is the second, then don't use mockup code for the first case in
your examples.

If it is the first case then you also do know the names of the arrays
and you can use your own old awkward method.
If it is the second case then either add the return array of each call
of fun immediately to the grand total as several people already sugested
in this thread or create an AoA to store the results from all fun calls
and then loop through that array at the end to sum up the results.
Any ideas/Suggestions?

You got pleanty of those already.
Sample code will help.

Tit for tat! You provide good sample code instead of mockup pseudo-code
and we may be able to help with more than general information.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top