how to join array into array
am 06.05.2007 12:52:46 von Tradeorganizer
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
Re: how to join array into array
am 08.05.2007 21:58:52 von Paul Lalli
On May 6, 6:52 am, 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)
mmmm, no. Sorry, not possible. I don't know what you *actually*
have, but you don't have that. Each element of an array holds a
single scalar value. They do not hold lists. Perhaps you mean that
each element of your array contains a reference to an array with those
values? That would be:
$x[0] = [1, 2, 3, 4];
$x[1] = [2, 3, 4, 5];
etc.
If you've actually typed what you have into your code, you've actually
made these assignments:
$x[0] = 4;
$x[1] = 5;
$x[31] = 1;
because the comma operator in scalar context returns its last operand.
> 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)
See above. Same issue.
> 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.
Perhaps this will get you on the right path....
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my (@x, @y);
$x[0] = [1, 2, 3, 4];
$x[1] = [2, 3, 4, 5];
$x[2] = [3, 4, 5, 6];
$y[0] = [2,2,3,4];
$y[1] = [2,3,3,5];
$y[2] = [2,4,3,6];
$y[3] = [2,5,3,7];
$y[4] = [2,6,3,8];
$y[5] = [2,7,3,9];
my @combo;
my $i = 0;
foreach my $y_elem (@y) {
push @combo, [ @{$y_elem}, @{$x[$i]} ];
$i++;
$i = 0 if $i == @x;
}
print Dumper(\@combo);
> kindly help i know perl but could not find the loop idea for this
Mmm, no, I'm sorry, I don't think you know Perl as well as you think
you do. You need to read:
perldoc perlreftut
perldoc perllol
Paul Lalli