eol*_*old 12 arrays perl parameter-passing subroutine
我在子程序中传递和读取参数时遇到问题,该子程序预计会有两个数组.
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
Run Code Online (Sandbox Code Playgroud)
我已经看过并尝试过网上的几个例子,但它们都没有为我工作.
Axe*_*man 29
有两种方法可以做到这一点:
但在我讨论这些之前 - 如果你在问题中显示的是关于你想做什么的程度 - 让我建议 List::MoreUtils::pairwise
所以,你要写这个:
my @sum = two_array_sum( @a, @b )
Run Code Online (Sandbox Code Playgroud)
你只需写下:
my @sum = pairwise { $a + $b } @a, @b;
Run Code Online (Sandbox Code Playgroud)
这就像push.(就像push它要求@对某些东西有印记一样)
sub two_array_sub (\@\@) {
my ( $aref, $bref ) = @_;
...
}
Run Code Online (Sandbox Code Playgroud)
那样做的时候
two_array_sub( @a, @b );
Run Code Online (Sandbox Code Playgroud)
有用.通常情况下,它只会在您的子组中显示为一个长列表.它们不适合所有人,正如您在下面的讨论中所看到的那样.
这就是每个人向你展示的方式.
some_sub( \@a, \@b );
Run Code Online (Sandbox Code Playgroud)
他们很挑剔.如果您有refs,这将不起作用:
two_array_sub( $arr_ref, $brr_ref );
Run Code Online (Sandbox Code Playgroud)
你必须像这样传递它们:
two_array_sub( @$arr_ref, @$brr_ref );
Run Code Online (Sandbox Code Playgroud)
但是,因为使用深度嵌套的数组使得"数组表达式"变得非常难看,我经常避免Perl的烦躁,因为你可以通过将它放在"字符类"构造中来重载Perl将要采用的引用类型.\[$@]表示引用可以是标量或数组.
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;
...
}
Run Code Online (Sandbox Code Playgroud)
所有这些工作:
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} );
Run Code Online (Sandbox Code Playgroud)
然而,Perl对此仍然很挑剔......出于某种原因:
new_two_array_sub( \@a, $b );
OR
new_two_array_sub( $a, [ 1..3 ] );
Run Code Online (Sandbox Code Playgroud)
或者任何其他"构造函数"仍然可以被视为对数组的引用.幸运的是,您可以使用旧的Perl 4关闭Perl &
&new_two_array_sub( \@a, [ 1..3 ] );
Run Code Online (Sandbox Code Playgroud)
然后子中的多路复用代码负责处理两个数组引用.
将对数组的引用传递给函数:
two_array_sum( \@a, \@b )
Run Code Online (Sandbox Code Playgroud)
并且不使用a或b作为变量名,因为$a和$b特殊(排序).
我会引用,man perlref但你应该读一遍:
Making References
References can be created in several ways.
1. By using the backslash operator on a variable, subroutine, or
value. (This works much like the & (address-of) operator in C.)
This typically creates another reference to a variable, because
there's already a reference to the variable in the symbol table.
But the symbol table reference might go away, and you'll still have
the reference that the backslash returned. Here are some examples:
$scalarref = \$foo;
$arrayref = \@ARGV;
$hashref = \%ENV;
$coderef = \&handler;
$globref = \*foo;
Run Code Online (Sandbox Code Playgroud)
...
Using References
That's it for creating references. By now you're probably dying to
know how to use references to get back to your long-lost data. There
are several basic methods.
1. Anywhere you'd put an identifier (or chain of identifiers) as part
of a variable or subroutine name, you can replace the identifier
with a simple scalar variable containing a reference of the correct
type:
$bar = $$scalarref;
push(@$arrayref, $filename);
$$arrayref[0] = "January";
$$hashref{"KEY"} = "VALUE";
&$coderef(1,2,3);
print $globref "output\n";
Run Code Online (Sandbox Code Playgroud)