通过两个或更多阵列来一个Perl子程序 [英] Passing two or more arrays to a Perl subroutine

查看:118
本文介绍了通过两个或更多阵列来一个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. 引用

但在此之前我讨论这些 - 如果你在​​你的问题表明的是关于你想做什么程度 - 让我建议的 列表:: MoreUtils ::配对

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;

通过原型

这就像。 (就如同它要求有一个 @ 印记的的东西的)

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 ) = @_;
    ...
}

这样,当你做到这一点。

That way when you do this

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 );

不过,由于制作数组前pressions得到的真正的使用数组丑陋迅速嵌套很深,我常常避免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;
    ...
}

所以,所有这些工作:

So all these work:

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 ] );

或任何其他构造仍然可以被看作是一个数组的引用。

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 ] );

然后在子的MUX-ING code需要处理两个数组引用照顾。

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

这篇关于通过两个或更多阵列来一个Perl子程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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