-1

Let's say i have 2 arrays.

@a1= qw (1, 2, 3);
@a2= qw (a, b, c);

How can i print the items from these arrays in the following manner:

1 a
2 b
3 c

Edit: it should be noted that neither the quantity of items nor the content of the arrays is known.

4

3 回答 3

3

尝试这样做

use Modern::Perl;

my @a1 = qw (1 2 3);
my @a2 = qw (a b c);
say join " ", ($a1[$_], $a2[$_]) for 0..$#a1;

,(使用时不要放qw//

如果你喜欢简单的旧风格:

my @a1 = qw (1 2 3);
my @a2 = qw (a b c);
print join(" ", ($a1[$_], $a2[$_])) . "\n" for 0..$#a1;
于 2013-10-26T21:41:22.070 回答
2
my @a1 = qw (1 2 3);
my @a2 = qw (a b c);
print "$a1[$_] $a2[$_]\n" for 0..$#a1;

假设它们的长度相同。

于 2013-10-26T21:46:50.267 回答
0

要解决数组的未知大小,您可以找到数组的长度,并在您选择的循环结构中打印出如下内容:

//pseudocode

print(array1[0] + array2[0]);

只要数组长度相等,您就可以这样做。

于 2013-10-26T21:42:10.870 回答