Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to join array into array

Reply
Thread Tools

how to join array into array

 
 
Tradeorganizer
Guest
Posts: n/a
 
      05-06-2007
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

 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      05-06-2007
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,
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-06-2007
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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      05-06-2007
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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      05-06-2007
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:umper;

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
 
Reply With Quote
 
Tradeorganizer
Guest
Posts: n/a
 
      05-06-2007
On May 6, 9:49 am, Tad McClellan <t...@augustmail.com> wrote:
> Tradeorganizer <tradeorgani...@gmail.com> 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:umper;
>
> 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
> t...@augustmail.com 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

 
Reply With Quote
 
Tradeorganizer
Guest
Posts: n/a
 
      05-06-2007
On May 6, 8:19 am, "John W. Krahn" <some...@example.com> 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)AR RAY(0x1c6545ARRAY(0x1c654c4)
what does it mean and how to get value printed for output file. kindly
help

Regards

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      05-06-2007
Tradeorganizer wrote:

> ARRAY(0x1c653b0)ARRAY(0x1c653ec)ARRAY(0x1c6544c)AR RAY(0x1c6545ARRAY(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;
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      05-06-2007
Tradeorganizer wrote:
> i dont know where i am wrong i am getting ouput value as below
> ARRAY(0x1c653b0)ARRAY(0x1c653ec)ARRAY(0x1c6544c)AR RAY(0x1c6545ARRAY(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.
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      05-06-2007
Tradeorganizer wrote:
>
> On May 6, 9:49 am, Tad McClellan <t...@augustmail.com> 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:umper's formating options.

perldoc Data:umper



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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
join array elements into a string Perl Lover Perl Misc 8 01-06-2007 11:08 PM
You must join New York Paid To Read !!!!!!! NewYork PTR is online!!! JOIN NOW!!!!!! Alan Silver ASP .Net 0 06-05-2006 03:27 PM
cheapest way to join several XML into a single one ... (XSLT vs JDOM) nospawn Java 3 04-07-2006 11:37 PM
list.join()... re.join()...? Do they exist? (newbie questions...) googleboy Python 1 10-01-2005 12:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57