如何在perl中合并两个数组,交替每个数组的值

时间:2022-06-01 16:45:01

suppose i have 2 arrays like mentioned below

假设我有2个如下所述的阵列

@a1["Vinay", Raj, harry];
@b1["dude","rock"];

After merging i want to have result like this

合并之后我想得到这样的结果

[
    Vinay
    dude
    Vinay
    rock
    Raj
    dude
    Raj
    rock
    harry
    dude
    harry
    rock
]

basically i want to merge the each index values of array1 to all the index values of array2 2

基本上我想将array1的每个索引值合并到array2 2的所有索引值

Adding to the above question i have another query

添加到上面的问题我有另一个问题

hi for the same question above how to merge 2 arrays at particular array index, for example i have 2 arrays of each 160 elements , now i want to merge array at every 5th element in sets, is that possible..?

嗨,对于上面相同的问题如何在特定的数组索引合并2个数组,例如我有每个160个元素的2个数组,现在我想在集合中的每个第5个元素合并数组,这是可能的吗?

Thanks, Vinay

谢谢,Vinay

6 个解决方案

#1


10  

Here is one way to do it. May not be the best perl syntax, though. The double for loop means you will be pushing every combination of the two arrays into the merged array. I've tried to set it up so that the result is in the order you specified in the question.

这是一种方法。但是,它可能不是最好的perl语法。 double for循环意味着您将把两个数组的每个组合推送到合并数组中。我试图设置它,以便结果按照您在问题中指定的顺序。

my @merged;

for my $a ( @a1 ) {
  for my $b ( @b1 ) {
    push( @merged, ($a, $b) );
  }
}

#2


52  

Just make a new array:

只需创建一个新数组:

my @merged = (@a1, @b1);

Here's a complete example:

这是一个完整的例子:

my @a1 = ("foo", "bar");
my @a2 = ("baz", "spam");

my @merged = (@a1, @a2);

print $merged[3]; #=> "spam"

EDIT: I missed the ordering requirement. You just need to zip them together, which you can do with List::MoreUtils:

编辑:我错过了订购要求。你只需要将它们压缩在一起,你可以使用List :: MoreUtils:

use List::MoreUtils qw(zip);
use Data::Dumper qw(Dumper);

my @a1 = ("Vinay", "Raj", "harry");
my @a2 = ("dude", "rock");

my @merged = zip(@a1, @a2);

print Dumper(\@merged);

#3


4  

Here's another option:

这是另一种选择:

use strict;
use warnings;
use Data::Dumper;

my $a = join ',', qw/Vinay Raj harry/;
my $b = join ',', qw/dude rock/;

my @merged = map { split '-' } glob "{$a}-{$b}";

print Dumper \@merged;

Output:

输出:

$VAR1 = [
          'Vinay',
          'dude',
          'Vinay',
          'rock',
          'Raj',
          'dude',
          'Raj',
          'rock',
          'harry',
          'dude',
          'harry',
          'rock'
        ]

#4


4  

First off, don't use barewords, and as noted in jmdeldin's comment, always place use strict; use warnings; at the start of a script.

首先,不要使用裸字,并且如jmdeldin的注释中所述,始终使用strict strict;使用警告;在脚本的开头。

So how to make those lists @a1 and @b1? Either use quotes:

那么如何制作这些列表@ a1和@ b1?使用引号:

my @a1 = ('Vinay', 'Raj', 'harry');
my @a2 = ('dude', 'rock');

Or use qw:

或者使用qw:

my @a1 = qw(Vinay Raj harry);
my @a2 = qw(dude rock);

How to "merge" these as specified? One way is to use a pair of nested loops. Much more succinctly, use a map of a map:

如何按指定“合并”这些?一种方法是使用一对嵌套循环。更简洁地说,使用地图的地图:

my @merged = map {my $outer = $_; map {($outer, $_)} @b1} @a1;

You don't need any special capabilities from perl 5.12. Just take advantage of the fact that lists collapse.

您不需要perl 5.12的任何特殊功能。只是利用列表崩溃的事实。

#5


1  

It could be done atrociously in one line:

它可能在一行中非常糟糕地完成:

@merged = grep {not ref} map {[our $t = $_], map {$t,$_} @b1} @a1;

and a bit less atrociously with a push inside

推进内部有点不太糟糕

map {our $t = $_, map {push(@merged, $t, $_)} @b1} @a1;

#6


0  

Use Set::CrossProduct from CPAN.

使用CPAN中的Set :: CrossProduct。

The zip from List::MoreUtils zips elements at corresponding (and not cross) indices of its argument lists.

List :: MoreUtils的zip在其参数列表的相应(而非交叉)索引处拉链元素。

#1


10  

Here is one way to do it. May not be the best perl syntax, though. The double for loop means you will be pushing every combination of the two arrays into the merged array. I've tried to set it up so that the result is in the order you specified in the question.

这是一种方法。但是,它可能不是最好的perl语法。 double for循环意味着您将把两个数组的每个组合推送到合并数组中。我试图设置它,以便结果按照您在问题中指定的顺序。

my @merged;

for my $a ( @a1 ) {
  for my $b ( @b1 ) {
    push( @merged, ($a, $b) );
  }
}

#2


52  

Just make a new array:

只需创建一个新数组:

my @merged = (@a1, @b1);

Here's a complete example:

这是一个完整的例子:

my @a1 = ("foo", "bar");
my @a2 = ("baz", "spam");

my @merged = (@a1, @a2);

print $merged[3]; #=> "spam"

EDIT: I missed the ordering requirement. You just need to zip them together, which you can do with List::MoreUtils:

编辑:我错过了订购要求。你只需要将它们压缩在一起,你可以使用List :: MoreUtils:

use List::MoreUtils qw(zip);
use Data::Dumper qw(Dumper);

my @a1 = ("Vinay", "Raj", "harry");
my @a2 = ("dude", "rock");

my @merged = zip(@a1, @a2);

print Dumper(\@merged);

#3


4  

Here's another option:

这是另一种选择:

use strict;
use warnings;
use Data::Dumper;

my $a = join ',', qw/Vinay Raj harry/;
my $b = join ',', qw/dude rock/;

my @merged = map { split '-' } glob "{$a}-{$b}";

print Dumper \@merged;

Output:

输出:

$VAR1 = [
          'Vinay',
          'dude',
          'Vinay',
          'rock',
          'Raj',
          'dude',
          'Raj',
          'rock',
          'harry',
          'dude',
          'harry',
          'rock'
        ]

#4


4  

First off, don't use barewords, and as noted in jmdeldin's comment, always place use strict; use warnings; at the start of a script.

首先,不要使用裸字,并且如jmdeldin的注释中所述,始终使用strict strict;使用警告;在脚本的开头。

So how to make those lists @a1 and @b1? Either use quotes:

那么如何制作这些列表@ a1和@ b1?使用引号:

my @a1 = ('Vinay', 'Raj', 'harry');
my @a2 = ('dude', 'rock');

Or use qw:

或者使用qw:

my @a1 = qw(Vinay Raj harry);
my @a2 = qw(dude rock);

How to "merge" these as specified? One way is to use a pair of nested loops. Much more succinctly, use a map of a map:

如何按指定“合并”这些?一种方法是使用一对嵌套循环。更简洁地说,使用地图的地图:

my @merged = map {my $outer = $_; map {($outer, $_)} @b1} @a1;

You don't need any special capabilities from perl 5.12. Just take advantage of the fact that lists collapse.

您不需要perl 5.12的任何特殊功能。只是利用列表崩溃的事实。

#5


1  

It could be done atrociously in one line:

它可能在一行中非常糟糕地完成:

@merged = grep {not ref} map {[our $t = $_], map {$t,$_} @b1} @a1;

and a bit less atrociously with a push inside

推进内部有点不太糟糕

map {our $t = $_, map {push(@merged, $t, $_)} @b1} @a1;

#6


0  

Use Set::CrossProduct from CPAN.

使用CPAN中的Set :: CrossProduct。

The zip from List::MoreUtils zips elements at corresponding (and not cross) indices of its argument lists.

List :: MoreUtils的zip在其参数列表的相应(而非交叉)索引处拉链元素。