对于多个数组

发布于 2024-11-14 22:19:50 字数 268 浏览 6 评论 0原文

在Scheme中,您可以使用for-each迭代多个列表:

> (for-each (lambda (a b) (display (+ a b)) (newline)) '(10 20 30) '(1 2 3))
11
22
33
> 

我知道在Perl中您可以使用for迭代单个列表。像方案示例中那样迭代多个列表的好方法是什么?

我对 Perl 5 或 6 的答案感兴趣。

In Scheme you can iterate over multiple lists in with for-each:

> (for-each (lambda (a b) (display (+ a b)) (newline)) '(10 20 30) '(1 2 3))
11
22
33
> 

I know that in Perl you can use for to iterate over a single list. What's a good way to iterate over multiple lists as in the Scheme example?

I'm interested in answers for Perl 5 or 6.

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

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

发布评论

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

评论(8

冰魂雪魄 2024-11-21 22:19:50

在 Perl 6 中,Zip 运算符是最好的选择。如果您想获取两个值(而不是直接计算总和),则可以使用不带加号的值:

for (10, 11, 12) Z (1, 2, 3) -> $a, $b {
    say "$a and $b";
}

In Perl 6, the Zip operator is the nicest choice. If you want to get both values (and not compute the sum directly), you can use it without the plus sign:

for (10, 11, 12) Z (1, 2, 3) -> $a, $b {
    say "$a and $b";
}
残龙傲雪 2024-11-21 22:19:50

在 Perl 5 中,您可以使用模块 List::MoreUtils。可以使用成对的方式,也可以使用each_array 返回的迭代器(可以并行遍历两个以上的数组)。

use 5.12.0;
use List::MoreUtils qw(pairwise each_array);

my @one = qw(a b c d e);
my @two = qw(q w e r t);

my @three = pairwise {"$a:$b"} @one, @two;

say join(" ", @three);

my $it = each_array(@one, @two);
while (my @elems = $it->()) {
    say "$elems[0] and $elems[1]";
}

In Perl 5 you can use the module List::MoreUtils. Either with pairwise or with the iterator returned by each_array (which can take more than two arrays to iterate through in parallel).

use 5.12.0;
use List::MoreUtils qw(pairwise each_array);

my @one = qw(a b c d e);
my @two = qw(q w e r t);

my @three = pairwise {"$a:$b"} @one, @two;

say join(" ", @three);

my $it = each_array(@one, @two);
while (my @elems = $it->()) {
    say "$elems[0] and $elems[1]";
}
樱花细雨 2024-11-21 22:19:50

使用 Zip 运算符,您可以实现您使用 Scheme 所做的事情:

> .say for (10, 20, 30) Z+ (1, 2, 3)
11
22
33

请参阅 http://perlcabal.org /syn/S03.html#Zip_operators

With the Zip operator you can achieve what you're doing with Scheme:

> .say for (10, 20, 30) Z+ (1, 2, 3)
11
22
33

See http://perlcabal.org/syn/S03.html#Zip_operators

你是暖光i 2024-11-21 22:19:50

如果您确定数组的索引大小相同,则可以迭代它们:

foreach( 0 .. $#array1 ) {
  print $array1[$_] + $array2[$_], "\n";
}

You could just iterate over the indices of the arrays, if you're sure they're the same size:

foreach( 0 .. $#array1 ) {
  print $array1[$_] + $array2[$_], "\n";
}
通知家属抬走 2024-11-21 22:19:50

Algorithm::Loops 提供了一个 MapCar 函数,用于迭代多个数组(具有以不同方式处理不平等问题的变体)大小的数组)。

Algorithm::Loops offers a MapCar function for iterating over multiple arrays (with variants that deal differently with unequally sized arrays).

吻安 2024-11-21 22:19:50

一种方法是:

sub for_each
{
    my $proc = shift ;

    my $len = @{$_[0]} ;

    for ( my $i = 0 ; $i < $len ; $i++ )
    {
        my @args = map $_->[$i] , @_ ;

        &$proc ( @args ) ;
    }
}

for_each sub { say $_[0] + $_[1] } , ([10,20,30],[1,2,3])

使用 List::MoreUtils 中的 each_arrayref

sub for_each
{
    my $proc = shift ;

    my $it = each_arrayref ( @_ ) ;

    while ( my @elts = $it->() ) { &$proc ( @elts ) } ;
}

for_each sub { say $_[0] + $_[1] } , ([10,20,30],[1,2,3])

感谢 Alex 指出 List::MoreUtils

One way is:

sub for_each
{
    my $proc = shift ;

    my $len = @{$_[0]} ;

    for ( my $i = 0 ; $i < $len ; $i++ )
    {
        my @args = map $_->[$i] , @_ ;

        &$proc ( @args ) ;
    }
}

for_each sub { say $_[0] + $_[1] } , ([10,20,30],[1,2,3])

Using each_arrayref from List::MoreUtils:

sub for_each
{
    my $proc = shift ;

    my $it = each_arrayref ( @_ ) ;

    while ( my @elts = $it->() ) { &$proc ( @elts ) } ;
}

for_each sub { say $_[0] + $_[1] } , ([10,20,30],[1,2,3])

Thanks to Alex for pointing out List::MoreUtils.

咋地 2024-11-21 22:19:50

只要问题很简单,只需添加(/乘法、除法……)即可
数组不是数组的数组,您还可以使用超级运算符来完成您的任务:(

<1 2 3> «+» <10 20 30>

当然这是 Perl6 答案)

如果您无法访问法语引号 «»,您可以将其重写为

<1 2 3> <<+>> <10 20 30>

As long as the question is as simple as just adding (/multiplying, dividing,…) and the
Arrays are no Arrays of Arrays, you also could use the hyper-operators for your task:

<1 2 3> «+» <10 20 30>

(of course this is a Perl6 answer)

if you don't have access to the French quotes «», you could rewrite it as

<1 2 3> <<+>> <10 20 30>
寄人书 2024-11-21 22:19:50

一种解决方案(有很多可供选择)可能如下所示:

my @argle = (10, 20, 30);
my @bargle = (1, 2, 3);
do {
    my $yin = shift @argle;
    my $yang = shift @bargle;
    say $yin + $yang;
} while (@argle && @bargle);

我感觉您正在询问 foreach,它可能如下所示:

my @argle = (10, 20, 30);
my @bargle = (1, 2, 3);
for my $yin (@argle) {
    my $yang = shift @bargle;
    say $yin + $yang;
}

但在这种情况下它并不那么顺利。如果任一数组较短会发生什么?

One solution (there are many to choose from) might look like this:

my @argle = (10, 20, 30);
my @bargle = (1, 2, 3);
do {
    my $yin = shift @argle;
    my $yang = shift @bargle;
    say $yin + $yang;
} while (@argle && @bargle);

I sense that you are asking about foreach, which might look like this:

my @argle = (10, 20, 30);
my @bargle = (1, 2, 3);
for my $yin (@argle) {
    my $yang = shift @bargle;
    say $yin + $yang;
}

But it is not as smooth in this case. What happens if either array is shorter?

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