0

I have 3 arrays in Perl:

my @col1 = ('A', 'B', 'C', 'D');
my @col2 = ('2', '4', '6', '8');
my @col3 = ('Abc', 'Ghy', 'Tgh', 'Yth');

How would I combine these 3 arrays to be side by side so the result will look like

col1 | col2 | col3


A 2 Abc

B 4 Ghy

C 6 Tgh

D 8 Yth

4
  • I want to create 1 array Commented Sep 24, 2015 at 19:02
  • Are they always going to be the same length? Commented Sep 24, 2015 at 19:04
  • yes they will always be of equal length Commented Sep 24, 2015 at 19:04
  • 1
    Here is an example that creates a 2D array @c: perl -E '@a=(1,2,3); @b=(4,5,6); for(0..$#a) {push @c, [$a[$_], $b[$_]]} for (@c) {say join " ", @$_}' Commented Sep 24, 2015 at 19:08

2 Answers 2

7

Update

I hope it's clear how to generate a 2D array given the same function. Here it is just in case

use strict;
use warnings;

use List::MoreUtils qw/ each_array /;

my @col1 = qw/ A   B   C   D   /;
my @col2 = qw/ 2   4   6   8   /;
my @col3 = qw/ Abc Ghy Tgh Yth /;

my $iter = each_array @col1, @col2, @col3;

my @result;
while ( my @row = $iter->() ) {
    push @result, \@row;
}

use Data::Dump;
dd \@result;

output

[
  ["A", 2, "Abc"],
  ["B", 4, "Ghy"],
  ["C", 6, "Tgh"],
  ["D", 8, "Yth"],
]

Or if you'd rather do it without using a non-core module then this will do as you ask

use strict;
use warnings;

use List::Util qw/ max /;

my @col1 = qw/ A   B   C   D   /;
my @col2 = qw/ 2   4   6   8   /;
my @col3 = qw/ Abc Ghy Tgh Yth /;

my @cols = \(@col1, @col2, @col3);

my @result;
for my $i ( 0 .. max map $#$_, @cols ) {
    push @result, [ map $_->[$i], @cols ];
}

The resulting @result is identical


Original post

I suggest that you make use of the each_array function from List::MoreUtils. Given a list of arrays, it returns an iterator function which will hand back the next set of values from those arrays each time it is called

Here is some example code that uses your own data

use strict;
use warnings;

use List::MoreUtils qw/ each_array /;

my @col1 = qw/ A   B   C   D   /;
my @col2 = qw/ 2   4   6   8   /;
my @col3 = qw/ Abc Ghy Tgh Yth /;

my $iter = each_array @col1, @col2, @col3;

while ( my @row = $iter->() ) {
    print "@row\n";
}

output

A 2 Abc
B 4 Ghy
C 6 Tgh
D 8 Yth
Sign up to request clarification or add additional context in comments.

Comments

6

Well, one is just 'stick them together':

my @combined = ( \@col1, \@col2, \@col3); 

Because your output is just a question of displaying it.

However alternatively:

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

my @col1 = ( 'A',   'B',   'C',   'D' );
my @col2 = ( '2',   '4',   '6',   '8' );
my @col3 = ( 'Abc', 'Ghy', 'Tgh', 'Yth' );

my @combined;
while ( @col1 or @col2 or  @col3 ) {
    push( @combined, [ map { shift ( @$_ ) // '' } (\@col1, \@col2, \@col3 ) ] );
}


print Dumper \@combined;

Will rotate your array, such that you can:

print join ( "|", @$_ ),"\n" for @combined;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.