如何将数组的每个元素添加到另一个数组的每个元素? [英] How to add each element of array to another array's each element?

查看:403
本文介绍了如何将数组的每个元素添加到另一个数组的每个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些数组集合,每个数组都有两个元素。

I have these sets of arrays which has two elements for each.

@a = ("a", "b");

@i = (1, 2);

@s = ( "\\!", "\?");

如何生成结果以使其返回

How do I make the result such that it'll return


a1!,  b2?

我需要它们成为数组的新集合,如

And I need them to be a new set of an array like

@new =(a1!,b2?)

我写了答案输出的代码

$i = length(@a);

for (0..$1) {

    @array = push(@array, @a[$i], @s[$i];

}

print @array;

但是,仅返回

syntax error at pra.pl line 10, near "];"

谢谢。

推荐答案

基本您有一个好主意,可以同时使用数组的索引进行迭代,但是代码有许多基本错误,并且也不能像示例所示那样进行,我建议您首先全面了解现代的Perl教程。

The basic idea you have is good, to iterate simultaneously using index of an array. But the code has many elementary errors and it also doesn't do what the examples show. I suggest to first make a thorough pass through a modern and reputable Perl tutorial.

示例表明您要连接(请参阅 运算符

The examples indicate that you want to concatenate (see . operator) elements at each index

use warnings;
use strict;
use feature 'say';

my @a1 = ('a', 'b');
my @a2 = (1, 2);
my @a3 = ('!', '?');

my @res;
foreach my $i (0..$#a1) {
    push @res, $a1[$i] . $a2[$i] . $a3[$i];
}

say for @res;

其中 $#a1 是数组 @ a1 的最后一个元素。假设所有数组的大小相同,并且定义了所有元素。

where $#a1 is the index of the last element of array @a1. This assumes that all arrays are of the same size and that all their elements are defined.

可以使用地图在一条语句中

my @res = map { $a1[$_] . $a2[$_] . $a3[$_] } 0..$#a1;

具有相同的严肃假设。即使您知道他们的身份,您是否肯定在每次运行任何数据时都知道 ?有关可靠的方法,请参见由mwp回答

with the same, serious, assumptions. Even if you knew they held, do you know for sure, in every run on any data? For a robust approach see the answer by mwp.

有也是 each_array 来自列表:: MoreUtils ,为所有数组提供同时迭代器

There is also each_array from List::MoreUtils, providing a "simultaneous iterator" for all arrays

my $ea = each_array(@a1, @a2, @a3);

my @res;
while ( my ($e1, $e2, $e3) = $ea->() ) {
    push @res, $e1 . $e2 . $e3
}

这对于更复杂的处理非常有用。

which is really useful for more complex processing.

快速入门


  • 始终具有个使用警告; 使用strict; 在程序开始时。它们将捕获许多错误,否则将花费很多时间和神经。

  • Always have use warnings; and use strict; at the beginning of your programs. They will catch many errors that would otherwise take a lot of time and nerves.

不要使用单字母变量名称。我们很快就忘记了它们的含义,并且代码变得难以遵循,而且它们也很容易犯傻错误。

Don't use single-letter variable names. We quickly forget what they meant and the code gets hard to follow, and they make it way too easy to make silly mistakes.

数组的大小 length 给出的值。通常使用上下文获得-当数组分配给标量时,数字其元素的返回。对于索引上的迭代,有 $#ary ,即 @ary 的最后一个元素的索引。然后,索引列表为 0 .. $#ary ,使用范围( .. )运算符

Array's size is not given by length. It is normally obtained using context -- when an array is assigned to a scalar the number of its elements is returned. For iteration over indices there is $#ary, the index of the last element of @ary. Then the list of indices is 0 .. $#ary, using the range (..) operator

开头( $ @ )标识符(变量名称)的名称表示变量类型(标量,数组,哈希)。数组元素是标量,因此需要 $ - $ ary [0]

The sigil ($, @, %) at the beginning of an identifier (variable name) indicates the type of the variable (scalar, array, hash). An array element is a scalar so it needs $ -- $ary[0]

push 不返回数组元素,但它会在其第一个参数的数组中添加后面列表中的标量。

The push doesn't return array elements but it rather adds to the array in its first argument the scalars in the list that follows.

print @array; 打印数组元素之间没有任何内容。当您引用它时,会添加空格,打印 @ array\n; 。请注意方便的功能 ,这将添加新行。

The print @array; prints array elements without anything between them. When you quote it spaces are added, print "@array\n";. Note the handy feature say though, which adds the new line.

这篇关于如何将数组的每个元素添加到另一个数组的每个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆