how to join array into array

T

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
 
M

Michele Dondi

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
 
G

Gunnar Hjalmarsson

Michele said:
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...
 
J

John W. Krahn

Tradeorganizer said:
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
 
T

Tad McClellan

Tradeorganizer said:
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?
 
T

Tradeorganizer

Tradeorganizer said:
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?

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
 
T

Tradeorganizer

Tradeorganizer said:
I am having two array example below :

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[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

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
 
J

Joe Smith

Tradeorganizer said:
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;
 
J

Joe Smith

Tradeorganizer said:
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.
 
J

John W. Krahn

Tradeorganizer said:
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
 
J

John W. Krahn

Tradeorganizer said:
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
 
T

Tad McClellan

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?
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top