Merging Multiple Array elements

Discussion in 'Perl Misc' started by Pradeep Patra, Jan 15, 2012.

  1. 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
    Pradeep Patra, Jan 15, 2012
    #1
    1. Advertising

  2. 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: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
    Jürgen Exner, Jan 15, 2012
    #2
    1. Advertising

  3. 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 <> 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
    Pradeep Patra, Jan 15, 2012
    #3
  4. 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
    Wolf Behrenhoff, Jan 15, 2012
    #4
  5. On Jan 15, 11:48 pm, Wolf Behrenhoff
    <> 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.
    Pradeep Patra, Jan 15, 2012
    #5
  6. 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 <> 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
    Pradeep Patra, Jan 15, 2012
    #6
  7. 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
    Wolf Behrenhoff, Jan 15, 2012
    #7
  8. Pradeep Patra

    Willem Guest

    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
    Willem, Jan 15, 2012
    #8
  9. On Jan 16, 12:32 am, Wolf Behrenhoff
    <> 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.
    Pradeep Patra, Jan 15, 2012
    #9
  10. Pradeep Patra

    Willem Guest

    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
    Willem, Jan 15, 2012
    #10
  11. 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, 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ürgen Exner, Jan 15, 2012
    #11
  12. 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.


    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
    Jürgen Exner, Jan 15, 2012
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. P
    Replies:
    1
    Views:
    1,134
    Joe Kesselman
    Jul 7, 2006
  2. Alan
    Replies:
    1
    Views:
    412
    Victor Bazarov
    Jun 4, 2007
  3. PC Paul
    Replies:
    3
    Views:
    381
  4. frakie

    merging multiple static library

    frakie, Sep 7, 2007, in forum: C Programming
    Replies:
    2
    Views:
    722
    frakie
    Sep 10, 2007
  5. Rock

    merging multiple datasets to bind to a grid

    Rock, Nov 19, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    175
    Alvin Bruney [MVP]
    Nov 19, 2004
Loading...

Share This Page