views:

160

answers:

2

I have four arrays @A1, @A2, @A3, @A4 each with same number of elements, say 6. I want to create a 2 dimensional array which stores the values of two array elements I want output corresponding to following format. For example

Array1: A B C D E F
Array2: E F G H I J
Array3: Q W E R T Y
Array4: P O L I U G

Then the Matrix should be:

[0,0] = A E [0,1] = B F [0,2] = C G [0,3] = D H  [0,4] = E I  [0,5] = F J
[1,0] = Q P [1,1] = W O [1,2] = E L [1,3] = R I  [1,4] = T U  [1,5] = Y G

I will be doing the above for a lot number of arrays than just 4. So I will have to write some sort of loop.

Any suggestions on doing the same?

+2  A: 

Hope this helps.

use strict;
use warnings;

my @A1 = qw'A B C D E F';
my @A2 = qw'E F G H I J';
my @A3 = qw'Q W E R T Y';
my @A4 = qw'P O L I U G';

my @matrix;
$matrix[0][4] = [ $A1[3], $A2[3] ];

# Use one of the following

for(
  my $i=0;
  $i<@A1 || $i<@A2 || $i<@A3 || $i<@A4;
  $i++
){
  $matrix[0][$i+1] = [ $A1[$i], $A2[$i] ];
  $matrix[1][$i+1] = [ $A3[$i], $A4[$i] ];
}

# OR

for(
  my $i=0;
  $i<@A1 || $i<@A2 || $i<@A3 || $i<@A4;
  $i++
){
  push @{ $matrix[0] }, [ $A1[$i], $A2[$i] ];
  push @{ $matrix[1] }, [ $A3[$i], $A4[$i] ];
}

# OR

{
  my $i = 1;
  for my $elem (@A1){
    push @{ $matrix[0][$i++] }, $elem;
  }
  $i = 1;
  for my $elem (@A2){
    push @{ $matrix[0][$i++] }, $elem;
  }
  $i = 1;
  for my $elem (@A3){
    push @{ $matrix[1][$i++] }, $elem;
  }
  $i = 1;
  for my $elem (@A4){
    push @{ $matrix[1][$i++] }, $elem;
  }
}
# notice how $i is out of scope here

To print it out you could use Data::Dumper or YAML

use 5.010;

use Data::Dumper;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 1;
say Dumper \@matrix;

use YAML;
say Dump   \@matrix;

use Data::Dump 'dump';
say dump \@matrix;

Data::Dumper

[
  [
    [
      'A',
      'E'
    ],
    [
      'B',
      'F'
    ],
    [
      'C',
      'G'
    ],
    [
      'D',
      'H'
    ],
    [
      'E',
      'I'
    ],
    [
      'F',
      'J'
    ]
  ],
  [
    [
      'Q',
      'P'
    ],
    [
      'W',
      'O'
    ],
    [
      'E',
      'L'
    ],
    [
      'R',
      'I'
    ],
    [
      'T',
      'U'
    ],
    [
      'Y',
      'G'
    ]
  ]
]

YAML

---
-
  -
    - A
    - E
  -
    - B
    - F
  -
    - C
    - G
  -
    - D
    - H
  -
    - E
    - I
  -
    - F
    - J
-
  -
    - Q
    - P
  -
    - W
    - O
  -
    - E
    - L
  -
    - R
    - I
  -
    - T
    - U
  -
    - Y
    - G

Data::Dump

[
  [
    ["A", "E"],
    ["B", "F"],
    ["C", "G"],
    ["D", "H"],
    ["E", "I"],
    ["F", "J"],
  ],
  [
    ["Q", "P"],
    ["W", "O"],
    ["E", "L"],
    ["R", "I"],
    ["T", "U"],
    ["Y", "G"],
  ],
]
Brad Gilbert
i've updated the questio
shubster
@Brad: how can I print out the matrix for each case?
shubster
I checked and I am don't have 5.010 version of perl installed on the remote server. In that case how do I do it without using Data:: Dumper module?
shubster
You don't need `5.010` for Data::Dumper, only for `say`, which you could replace with `print Dumper(\@matrix), "\n"`.
Brad Gilbert
@Brad: that I was already doing. What I meant in my previous comment was is there any way to do this with out using Data::Dumper module, because that is not installed on the server. And also, how do write this output(matrix) to a file.
shubster
shubster: Data::Dumper is a core module, so it should be installed if you have Perl. Or you may have a broken Perl installation.
Alexandr Ciornii
Assuming it wasn't installed, you could put it in the same place you put the rest of your code, and '`use lib 'some/directory'`'.
Brad Gilbert
A: 

My wild guess yields the following program:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my @a1 = ( 10 .. 19 );
my @a2 = ( 20 .. 29 );

my @matrix;

for ( my $i = 0 ; $i < @a1 ; $i++ ) {
    push @matrix, [ $a1[$i], $a2[$i] ];
}

print Dumper \@matrix;
Alan Haggai Alavi
I've edited the question
shubster