how to join array into array

Discussion in 'Perl Misc' started by Tradeorganizer, May 6, 2007.

  1. Hi

    I am having two array example below :

    @x;
    @y;

    values in x and y are :

    $x[0] = (1,2,3,4)
    $x[1] = (2,3,4,5)
    .....
    till
    $x[31] = (1,1,1,1)

    now the value of y contains

    $y[0] = (2,2,3,4)
    $y[1] = (2,3,3,5)
    .....
    till
    $y[31] =(4,4,4,4)
    ......

    till
    $y[500] = (1,1,1,1)

    i want to make a loop where i can append the array y or create new
    array with having array x value fixed till record [31] ie end of array
    x plus value of array[y] till record [31] and then repeating again the
    value of array x to next records of array y i mean till next 31
    records of y

    so if you see the results output it should be lik below :

    @newxy

    $newxy[0] = (1,2,3,4,2,2,3,4)
    $newxy[1] = (2,3,4,5,2,3,3,5 )
    ......
    till $newxy[31] = (1,1,1,1, 4,4,4,4)

    and $newxy[32] values will look like (1,2,3,4,5,5,5,5) here its
    getting repeating value from array x till it finished 31 record and
    then restared.

    kindly help i know perl but could not find the loop idea for this

    Regards
    Tradeorganizer, May 6, 2007
    #1
    1. Advertising

  2. On 6 May 2007 03:46:31 -0700, Tradeorganizer
    <> wrote:

    >Hi


    Hi

    >I am having two array example below :
    >
    >@x;
    >@y;


    Yes...

    >values in x and y are :
    >
    >$x[0] = (1,2,3,4)
    >$x[1] = (2,3,4,5)


    You're missing the semicolons, btw.

    Anyway, this does not make much sense: you have a list assigned to a
    scalar. The above is perfectly equivalent to

    $x[0] = 4;
    $x[1] = 4;

    Perhaps you mean

    $x[0] = [1,2,3,4];
    $x[1] = [2,3,4,5];

    >i want to make a loop where i can append the array y or create new
    >array with having array x value fixed till record [31] ie end of array
    >x plus value of array[y] till record [31] and then repeating again the
    >value of array x to next records of array y i mean till next 31
    >records of y


    I can't understand.

    >so if you see the results output it should be lik below :
    >
    >@newxy
    >
    >$newxy[0] = (1,2,3,4,2,2,3,4)
    >$newxy[1] = (2,3,4,5,2,3,3,5 )
    >.....
    >till $newxy[31] = (1,1,1,1, 4,4,4,4)
    >
    >and $newxy[32] values will look like (1,2,3,4,5,5,5,5) here its
    >getting repeating value from array x till it finished 31 record and
    >then restared.


    Mild possibility of me understanding. Could you please post a reduced
    example with say @x having 3 entries, @y 4 and your desired @xy
    output?

    >kindly help i know perl but could not find the loop idea for this


    The above seems to suggest you don't know Perl very well, but I may be
    wrong. Anyway, you should show what you have tried thus far. That
    would help us to understand what you're really after.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, May 6, 2007
    #2
    1. Advertising

  3. Michele Dondi wrote:
    > On 6 May 2007 03:46:31 -0700, Tradeorganizer
    > <> wrote:
    >>kindly help i know perl but could not find the loop idea for this

    >
    > The above seems to suggest you don't know Perl very well, but I may be
    > wrong.


    In any case, the guy doesn't know Usenet very well. He multi-posted...

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 6, 2007
    #3
  4. Tradeorganizer wrote:
    >
    > I am having two array example below :
    >
    > @x;
    > @y;
    >
    > values in x and y are :
    >
    > $x[0] = (1,2,3,4)
    > $x[1] = (2,3,4,5)
    > ....
    > till
    > $x[31] = (1,1,1,1)
    >
    > now the value of y contains
    >
    > $y[0] = (2,2,3,4)
    > $y[1] = (2,3,3,5)
    > ....
    > till
    > $y[31] =(4,4,4,4)
    > .....
    >
    > till
    > $y[500] = (1,1,1,1)
    >
    > i want to make a loop where i can append the array y or create new
    > array with having array x value fixed till record [31] ie end of array
    > x plus value of array[y] till record [31] and then repeating again the
    > value of array x to next records of array y i mean till next 31
    > records of y
    >
    > so if you see the results output it should be lik below :
    >
    > @newxy
    >
    > $newxy[0] = (1,2,3,4,2,2,3,4)
    > $newxy[1] = (2,3,4,5,2,3,3,5 )
    > .....
    > till $newxy[31] = (1,1,1,1, 4,4,4,4)
    >
    > and $newxy[32] values will look like (1,2,3,4,5,5,5,5) here its
    > getting repeating value from array x till it finished 31 record and
    > then restared.
    >
    > kindly help i know perl but could not find the loop idea for this


    my @newxy;
    for my $index_y ( 0 .. $#y ) {
    push @newxy, [ @{ $x[ $index_y % @x ] }, @{ $y[ $index_y ] } ];
    }





    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
    John W. Krahn, May 6, 2007
    #4
  5. Tradeorganizer <> wrote:

    > values in x and y are :
    >
    > $x[0] = (1,2,3,4)



    Please speak Perl rather than English, when possible.

    $x[0] = [1,2,3,4];


    > $x[1] = (2,3,4,5)
    > ....
    > till
    > $x[31] = (1,1,1,1)



    Wouldn't having just 2 or 3 illustrate your problem just as
    well as having 32 of them?


    > now the value of y contains
    >
    > $y[0] = (2,2,3,4)
    > $y[1] = (2,3,3,5)
    > ....
    > till
    > $y[31] =(4,4,4,4)
    > .....
    >
    > till
    > $y[500] = (1,1,1,1)
    >
    > i want to make a loop where i can append the array y or create new
    > array with having array x value fixed till record [31] ie end of array
    > x plus value of array[y] till record [31] and then repeating again the
    > value of array x to next records of array y i mean till next 31
    > records of y



    If I understand you correctly, then I think this does it:

    ----------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;
    use Data::Dumper;

    my @x = (
    [1,2,3,4],
    [2,3,4,5],
    );

    my @y = (
    [2,2,3,4],
    [2,3,3,5],
    [3,4,5,6],
    [7,8,9,0],
    [1,1,1,1],
    );


    my @newxy;
    for ( my $xi = my $yi = 0; defined $y[$yi]; $xi++, $yi++ ) {
    push @newxy, [ @{ $x[$xi] }, @{ $y[$yi] } ];
    $xi = -1 if $xi == $#x; # roll back to zero
    }

    print Dumper \@newxy;
    ----------------------------



    > kindly help i know perl



    Then you should be able to speak Perl in your posts.

    Have you seen the Posting Guidelines that are posted here frequently?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 6, 2007
    #5
  6. On May 6, 9:49 am, Tad McClellan <> wrote:
    > Tradeorganizer <> wrote:
    > > values in x and y are :

    >
    > > $x[0] = (1,2,3,4)

    >
    > Please speak Perl rather than English, when possible.
    >
    > $x[0] = [1,2,3,4];
    >
    > > $x[1] = (2,3,4,5)
    > > ....
    > > till
    > > $x[31] = (1,1,1,1)

    >
    > Wouldn't having just 2 or 3 illustrate your problem just as
    > well as having 32 of them?
    >
    >
    >
    > > now the value of y contains

    >
    > > $y[0] = (2,2,3,4)
    > > $y[1] = (2,3,3,5)
    > > ....
    > > till
    > > $y[31] =(4,4,4,4)
    > > .....

    >
    > > till
    > > $y[500] = (1,1,1,1)

    >
    > > i want to make a loop where i can append the array y or create new
    > > array with having array x value fixed till record [31] ie end of array
    > > x plus value of array[y] till record [31] and then repeating again the
    > > value of array x to next records of array y i mean till next 31
    > > records of y

    >
    > If I understand you correctly, then I think this does it:
    >
    > ----------------------------
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    > use Data::Dumper;
    >
    > my @x = (
    > [1,2,3,4],
    > [2,3,4,5],
    > );
    >
    > my @y = (
    > [2,2,3,4],
    > [2,3,3,5],
    > [3,4,5,6],
    > [7,8,9,0],
    > [1,1,1,1],
    > );
    >
    > my @newxy;
    > for ( my $xi = my $yi = 0; defined $y[$yi]; $xi++, $yi++ ) {
    > push @newxy, [ @{ $x[$xi] }, @{ $y[$yi] } ];
    > $xi = -1 if $xi == $#x; # roll back to zero
    >
    > }
    >
    > print Dumper \@newxy;
    > ----------------------------
    >
    > > kindly help i know perl

    >
    > Then you should be able to speak Perl in your posts.
    >
    > Have you seen the Posting Guidelines that are posted here frequently?
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas


    Hi Tad ,

    Thanks for your reply , it worked , but the output i am getting is
    below :


    C:\perlprog>testlooprt.pl

    $VAR1 = [
    [
    1,
    2,
    3,
    4,
    2,
    2,
    3,
    4
    ],
    [
    2,
    3,
    4,
    5,
    2,
    3,
    3,
    5
    ],
    [
    1,
    2,
    3,
    4,
    3,
    4,
    5,
    6
    ],
    [
    2,
    3,
    4,
    5,
    7,
    8,
    9,
    0
    ],
    [
    1,
    2,
    3,
    4,
    1,
    1,
    1,
    1
    ]
    ];

    where as i need like
    $VAR1 = [ [ 1, 2, 3, 4, 2, 2, 3, 4 ],
    how can i do that.

    Regards
    Tradeorganizer, May 6, 2007
    #6
  7. On May 6, 8:19 am, "John W. Krahn" <> wrote:
    > Tradeorganizer wrote:
    >
    > > I am having two array example below :

    >
    > > @x;
    > > @y;

    >
    > > values in x and y are :

    >
    > > $x[0] = (1,2,3,4)
    > > $x[1] = (2,3,4,5)
    > > ....
    > > till
    > > $x[31] = (1,1,1,1)

    >
    > > now the value of y contains

    >
    > > $y[0] = (2,2,3,4)
    > > $y[1] = (2,3,3,5)
    > > ....
    > > till
    > > $y[31] =(4,4,4,4)
    > > .....

    >
    > > till
    > > $y[500] = (1,1,1,1)

    >
    > > i want to make a loop where i can append the array y or create new
    > > array with having array x value fixed till record [31] ie end of array
    > > x plus value of array[y] till record [31] and then repeating again the
    > > value of array x to next records of array y i mean till next 31
    > > records of y

    >
    > > so if you see the results output it should be lik below :

    >
    > > @newxy

    >
    > > $newxy[0] = (1,2,3,4,2,2,3,4)
    > > $newxy[1] = (2,3,4,5,2,3,3,5 )
    > > .....
    > > till $newxy[31] = (1,1,1,1, 4,4,4,4)

    >
    > > and $newxy[32] values will look like (1,2,3,4,5,5,5,5) here its
    > > getting repeating value from array x till it finished 31 record and
    > > then restared.

    >
    > > kindly help i know perl but could not find the loop idea for this

    >
    > my @newxy;
    > for my $index_y ( 0 .. $#y ) {
    > push @newxy, [ @{ $x[ $index_y % @x ] }, @{ $y[ $index_y ] } ];
    > }
    >
    > 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


    Hi John

    Thanks for help , the array seems to be running fine but i dont know
    where i am wrong i am getting ouput value as
    below
    ARRAY(0x1c653b0)ARRAY(0x1c653ec)ARRAY(0x1c6544c)ARRAY(0x1c65458)ARRAY(0x1c654c4)
    what does it mean and how to get value printed for output file. kindly
    help

    Regards
    Tradeorganizer, May 6, 2007
    #7
  8. Tradeorganizer

    Joe Smith Guest

    Tradeorganizer wrote:

    > ARRAY(0x1c653b0)ARRAY(0x1c653ec)ARRAY(0x1c6544c)ARRAY(0x1c65458)ARRAY(0x1c654c4)
    > what does it mean and how to get value printed for output file.


    As you found,
    print @newxy;
    or
    print "$_\n" for @newxy;
    does not work with arrays of arrays.

    (You do know you're working with an AoA, don't you?)

    You need to access each sub-array individually.

    for my $array_ref (@newxy) {
    my @sub_array = @$array_ref;
    print '(', (join ',', @sub_array), ")\n";
    }

    or

    print '(',join(','=>@$_),")\n" for @newxy;
    Joe Smith, May 6, 2007
    #8
  9. Tradeorganizer

    Joe Smith Guest

    Tradeorganizer wrote:
    > i dont know where i am wrong i am getting ouput value as below
    > ARRAY(0x1c653b0)ARRAY(0x1c653ec)ARRAY(0x1c6544c)ARRAY(0x1c65458)ARRAY(0x1c654c4)
    > what does it mean


    Read "perldoc perlreftut" (http://perldoc.perl.org/perlreftut.html).
    Look for this section:

    o If you try to use a reference like a string, you get
    strings like

    ARRAY(0x80f5dec) or HASH(0x826afc0)

    If you ever see a string that looks like this, you'll
    know you printed out a reference by mistake.
    Joe Smith, May 6, 2007
    #9
  10. Tradeorganizer wrote:
    >
    > On May 6, 9:49 am, Tad McClellan <> wrote:
    >>
    >> my @newxy;
    >> for ( my $xi = my $yi = 0; defined $y[$yi]; $xi++, $yi++ ) {
    >> push @newxy, [ @{ $x[$xi] }, @{ $y[$yi] } ];
    >> $xi = -1 if $xi == $#x; # roll back to zero
    >>
    >> }
    >>
    >> print Dumper \@newxy;

    >
    > Thanks for your reply , it worked , but the output i am getting is
    > below :
    >
    >
    > C:\perlprog>testlooprt.pl
    >
    > $VAR1 = [
    > [
    > 1,
    > 2,
    > 3,
    > 4,
    > 2,
    > 2,
    > 3,
    > 4
    > ],


    [ snip ]

    > [
    > 1,
    > 2,
    > 3,
    > 4,
    > 1,
    > 1,
    > 1,
    > 1
    > ]
    > ];
    >
    > where as i need like
    > $VAR1 = [ [ 1, 2, 3, 4, 2, 2, 3, 4 ],
    > how can i do that.


    Change Data::Dumper's formating options.

    perldoc Data::Dumper



    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
    John W. Krahn, May 6, 2007
    #10
  11. Tradeorganizer wrote:
    >
    > On May 6, 8:19 am, "John W. Krahn" <> wrote:
    >>
    >> my @newxy;
    >> for my $index_y ( 0 .. $#y ) {
    >> push @newxy, [ @{ $x[ $index_y % @x ] }, @{ $y[ $index_y ] } ];
    >> }

    >
    > Thanks for help , the array seems to be running fine but i dont know
    > where i am wrong i am getting ouput value as
    > below
    > ARRAY(0x1c653b0)ARRAY(0x1c653ec)ARRAY(0x1c6544c)ARRAY(0x1c65458)ARRAY(0x1c654c4)
    > what does it mean and how to get value printed for output file. kindly
    > help


    for my $array ( @newxy ) {
    print "@$array\n";
    }



    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
    John W. Krahn, May 6, 2007
    #11
  12. Tradeorganizer <> wrote:
    > On May 6, 9:49 am, Tad McClellan <> wrote:


    >> Have you seen the Posting Guidelines that are posted here frequently?
    >>
    >> --
    >> Tad McClellan SGML consulting
    >> Perl programming
    >> Fort Worth, Texas



    It is bad manners to quote .sigs.

    It is bad manners to quote an entire post when it is not needed
    for context.

    Have you seen the Posting Guidelines that are posted here frequently?


    > Thanks for your reply , it worked , but the output i am getting is
    > below :
    >
    >
    > C:\perlprog>testlooprt.pl
    >
    > $VAR1 = [
    > [
    > 1,
    > 2,
    > 3,
    > 4,
    > 2,
    > 2,
    > 3,
    > 4
    > ],



    > where as i need like
    > $VAR1 = [ [ 1, 2, 3, 4, 2, 2, 3, 4 ],
    > how can i do that.



    By modifying the program you've been given.

    What modifications have you tried?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 6, 2007
    #12
  13. On 6 May 2007 10:41:55 -0700, Tradeorganizer
    <> wrote:

    >$VAR1 = [
    > [
    > 1,
    > 2,
    > 3,
    > 4,
    > 2,
    > 2,
    > 3,
    > 4
    > ],

    <snip>
    >where as i need like
    >$VAR1 = [ [ 1, 2, 3, 4, 2, 2, 3, 4 ],


    AFAICS it's the same. But for visualization. Try ->Indent(0).


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, May 11, 2007
    #13
    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. nospawn
    Replies:
    3
    Views:
    467
    Alex Hunsley
    Apr 8, 2006
  2. Alan Silver
    Replies:
    0
    Views:
    878
    Alan Silver
    Jun 5, 2006
  3. googleboy
    Replies:
    1
    Views:
    918
    Benji York
    Oct 1, 2005
  4. Imran
    Replies:
    3
    Views:
    337
    red floyd
    Jul 9, 2007
  5. Perl Lover

    join array elements into a string

    Perl Lover, Jan 6, 2007, in forum: Perl Misc
    Replies:
    8
    Views:
    204
    Perl Lover
    Jan 6, 2007
Loading...

Share This Page