如何创建二维矩阵并从 Perl 中的数组提供信息?

发布于 2024-07-27 03:51:50 字数 522 浏览 4 评论 0原文

我有四个数组 @A1@A2@A3@A4,每个数组都有相同数量的元素,说 6。 我想创建一个二维数组来存储两个数组元素的值 我想要对应于以下格式的输出。 例如,

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

那么矩阵应该是:

[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

我将对大量数组执行上述操作,而不仅仅是 4 个数组。所以我将不得不编写某种循环。

有什么建议做同样的事情吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳哥哥 2024-08-03 03:51:50

希望这可以帮助。

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

要打印它,您可以使用 Data::DumperYAML

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"],
  ],
]

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"],
  ],
]
习ぎ惯性依靠 2024-08-03 03:51:50

我的疯狂猜测产生了以下程序:

#!/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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文