Keep It Simple, Stupid PerlMonks

### issue with print/join 2 arrays

by juanito23 (Novice)
 on Apr 29, 2014 at 16:20 UTC Need Help??

juanito23 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am struggling with an issue with printing 2 arrays. Let's say I have these 2 arrays:

```@array1 = [1, 2, 3, 1, 2, 3, 1, 2, 3 ]

@array2 = [blue, white, yellow, blue, white, yellow, blue, white, yell
+ow]

IS there a way so I can have an output like

```1 2 3   blue white yellow
1 2 3   blue white yellow
1 2 3   blue white yellow

so the idea would be printing 3 elements of each row and print them together. I can print wach array separated like this:

```while (@array1){
push (@array_wb, \$_[0]);
push (@array_wb, \$_[1] );
push (@array_wb, \$_[2] . "\n");
push (@array_wb, \$_[3]);
push (@array_wb, \$_[4] );
push (@array_wb, \$_[5] . "\n");
push (@array_wb, \$_[6]);
push (@array_wb, \$_[7] );
push (@array_wb, \$_[8] . "\n");

print (@array_wb);
}

So I can print both array separared, bue if I print both I get this:

```1 2 3
1 2 3
1 2 3
blue white yellow
blue white yellow
blue white yellow
I know this code is not ideal, but my goal here is to make a output print as I mentioned. Any ideas? Thanks!

Replies are listed 'Best First'.
Re: issue with print/join 2 arrays
by toolic (Bishop) on Apr 29, 2014 at 16:36 UTC
Slices
```use warnings;
use strict;

my @array1 = (1, 2, 3, 1, 2, 3, 1, 2, 3 );
my @array2 = qw(blue white yellow blue white yellow blue white yellow)
+;
my \$i = 0;
while (\$i < \$#array1) {
print "@array1[\$i .. \$i+2] ";
print "@array2[\$i .. \$i+2]";
print "\n";
\$i += 3;
}

__END__

1 2 3 blue white yellow
1 2 3 blue white yellow
1 2 3 blue white yellow
Re: issue with print/join 2 arrays
by BrowserUk (Patriarch) on Apr 29, 2014 at 16:33 UTC

```@array1 = ( 1, 2, 3, 1, 2, 3, 1, 2, 3 );;
@array2 = qw[blue white yellow blue white yellow blue white yellow];;

print join "\t", @array1[ \$_ .. \$_+2 ], @array2[ \$_ .. \$_+2 ] for map
+\$_*3, 0 .. ( \$#array1 / 3 );;
1       2       3       blue    white   yellow
1       2       3       blue    white   yellow
1       2       3       blue    white   yellow

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re: issue with print/join 2 arrays
by roux.tophe (Novice) on Apr 29, 2014 at 16:48 UTC
```@array1 = (1, 2, 3, 1, 2, 3, 1, 2, 3 );
@array2 = (blue, white, yellow, blue, white, yellow, blue, white, yell
+ow);
while (@array1) {
(@p1) = (shift(@array1), shift(@array1), shift(@array1));
(@p2) = (shift(@array2), shift(@array2), shift(@array2));
printf("%s %s %s   %s %s %s\n", (@p1), (@p2));
}
the idea is to prepare print parameters in @p1 and @p2 before printing them... should be nicer with variables declarations and quotes around bare words (colors...) hope it helps
Re: issue with print/join 2 arrays
by Not_a_Number (Prior) on Apr 29, 2014 at 18:08 UTC
```use 5.010;

my @array1 = ( 1, 2, 3, 1, 2, 3, 1, 2, 3 );
my @array2 = qw( blue white yellow blue white yellow blue white yellow
+ );

say join ' ', map splice( \$_, 0, 3 ), \@array1, \@array2 while @array1
+;

But beware, this is destructive to both arrays.

Although your solution is quite good and probably does what is wanted, I tend to prefer BrowserUk's, because it does not destroy the original array.

Edit: added the word "not" which was missing and gave an opposite meaning to the last part of my sentence. Thanks to Bloodnok++ for having pointed out that mistake.

... or, more particularly, BrowserUk's, because it doesn't destroy the array :-)

A user level that continues to overstate my experience :-))

Hi, I don't have that version of perl so cannot apply that solution so, the "say" is not recognized.

Any other way to make it work? My current version of perl is v5.8.8

Thanks!

In this simple case, just
```sub say { print @_, "\n" }
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: issue with print/join 2 arrays
by RichardK (Parson) on Apr 29, 2014 at 17:08 UTC

But what should happen when the length of the first array isn't a multiple of 3?

Hi!, thanks for all replies, but yes.. The arrays I will have to print have more than 1000 rows per 3 columns... Any way to do it then? One other thing.. What if the length of both arrays are different? Thanks!!

Just adapt the solution I gave in my other post:

say join ' ', map splice( \$_, 0, 3 ), \@array1, \@array2 while @array1 or @array2;

If you need to access your original arrays later, make copies and destroy these instead.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1084347]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-06-30 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?