将两个或多个数组传递给 Perl 子例程 [英] Passing two or more arrays to a Perl subroutine

查看:29
本文介绍了将两个或多个数组传递给 Perl 子例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在预期有两个数组的子例程中传递和读取参数时遇到问题.

I am having trouble passing and reading arguments inside subroutine which is expected to have two arrays.

sub two_array_sum { # two_array_sum ( (1 2 3 4), (2, 4, 0, 1) ) -> (3, 6, 3, 5)
  # I would like to use parameters @a and @b as simply as possible
}

# I would like to call two_array_sum here and pass two arrays, @c and @d

我在网上看到并尝试了几个例子,但没有一个对我有用.

I have seen and tried several examples from the web, but none of them worked for me.

推荐答案

有两种方法可以做到这一点:

There are two ways you can do this:

  1. 按原型
  2. 参考

但在我讨论这些之前——如果你在你的问题中展示的是你想做的事情的范围——让我建议 List::MoreUtils::pairwise

But before I discuss these--if what you show in your question is about the extent of what you want to do--let me suggest List::MoreUtils::pairwise

那么,你会在哪里写这个:

So, where you would write this:

my @sum = two_array_sum( @a, @b )

你只需这样写:

my @sum = pairwise { $a + $b } @a, @b;

按原型

这类似于push.(就像 push 一样,它要求在 something 上有一个 @ 标记)

By prototype

This works like push. (And just like push it demands to have a @ sigil on something)

sub two_array_sub (\@\@) { 
    my ( $aref, $bref ) = @_;
    ...
}

当你这样做时那样

two_array_sub( @a, @b );

它有效.而通常它只会作为一个长列表出现在您的子列表中.正如您将在我下面的讨论中看到的那样,它们并不适合所有人.

it works. Whereas normally it would just show up in your sub as one long list. They aren't for everybody as you'll see in my discussion below.

这就是每个人向您展示的方式.

That's the way that everybody is showing you.

some_sub( \@a, \@b );

关于原型

他们很挑剔.如果您有参考,这将不起作用:

About prototypes

They're finicky. This won't work if you have refs:

two_array_sub( $arr_ref, $brr_ref );

你必须像这样传递它们:

You have to pass them like this:

two_array_sub( @$arr_ref, @$brr_ref );

然而,因为使用嵌套很深的数组,使数组表达式"变得真的难看,我经常避免 Perl 的繁琐,因为你可以通过将它放在一个字符"中来重载 Perl 将采用的引用类型类"结构.\[$@] 表示引用可以是标量或数组.

However, because making "array expressions" gets really ugly quickly with arrays nested deep, I often avoid Perl's fussiness as you can overload the type of reference Perl will take by putting it in a "character class" construct. \[$@] means that the reference can either be a scalar or array.

sub new_two_array_sub (\[$@]\[$@]) { 
    my $ref = shift;
    my $arr = ref( $ref ) eq 'ARRAY' ? $ref : $$ref; # ref -> 'REF';
    $ref    = shift;
    my $brr = ref( $ref ) eq 'ARRAY' ? $ref : $$ref;
    ...
}

所以所有这些都有效:

new_two_array_sub( @a, $self->{a_level}{an_array} );
new_two_array_sub( $arr, @b );
new_two_array_sub( @a, @b );
new_two_array_sub( $arr, $self->{a_level}{an_array} );

然而,出于某种原因,Perl 仍然对此很挑剔……

However, Perl is still fussy about this... for some reason:

new_two_array_sub( \@a, $b );
OR 
new_two_array_sub( $a, [ 1..3 ] );

或任何其他仍可被视为对数组的引用的构造函数".幸运的是,您可以使用旧的 Perl 4 &

Or any other "constructor" that still could be seen as a reference to an array. Fortunately, you can shut Perl up about that with the old Perl 4 &

&new_two_array_sub( \@a, [ 1..3 ] );

然后 sub 中的 mux-ing 代码负责处理两个数组引用.

Then the mux-ing code in the sub takes care of handling two array references.

这篇关于将两个或多个数组传递给 Perl 子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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