我怎么能传递两个数组和字符串到Perl子? [英] How can I pass two arrays and a string to a Perl subroutine?

查看:94
本文介绍了我怎么能传递两个数组和字符串到Perl子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能传递两个数组和字符串到子?

下面就是我想要做的:

 使用严格的;
使用警告;我@x = QW(AAAA BBBB CCCC DDDD EEEE);
我@y = QW(1111 2222 3333 4444 5555);我的$ Z =你好;你好(@ X,@ Y,$ Z);出口(0);子你好{    我(@ X,@ Y,$ Z)= @_;    打印$ _ \\ n表示@x;
    打印$ _ \\ n;
    打印$ _ \\ n表示@y;
    打印$ _ \\ n;
    打印$ Z而言\\ N的;
}


输出:

  AAA
BBBB
CCCC
DDDD
EEEE
1111
2222
3333
4444
5555
你好
未初始化值$使用_在级联(。),或在test.pl第19行字符串。未初始化值$使用_在级联(。),或在test.pl第21行字符串。在连接(。)或字符串使用未初始化值$ Z中test.pl 22行。



解决方案

您需要通过每一个阵列作为参考的,否则你的 @x 在次会吞噬参数的整个阵列,使 @y 空arraay和 $ Z而言一个民主基金的价值。

这是因为逗号运算 - 在一个列表上下文 - 会变成 @x,@y,$ Z而言成由>后跟<$ C中的所有元素 @x @y ,然后值 $ Z而言;你的 @x 在子会吞噬参数的整个组合阵列,留下 @y 空数组和 $ Z而言一个民主基金的价值。

混乱的另一个可能的原因是,你命名的两个变量 @x ,尽管他们是完全相互独立的,由于作用域规则的事实。良好的做法是给它们命名不同的东西,以避免猜测你想用哪一个,例如调用子程序的第一个数组 @ X2

请注意,您可以传递数组中的两种方式之一引用 - 引用原始数组(实际传递按引用的方法),以及参考数组的一个副本 - 它会像你想你的origainal code的行为和值传递。

 使用严格的;使用警告;我@x = QW(AAAA BBBB CCCC DDDD EEEE);
我@y = QW(1111 2222 3333 4444 5555);
我的$ Z =你好;你好(\\ @ X,\\ @ Y,$ Z);#如果你想通过数组的副本的参考,
#这样就可以修改它的子程序里面没有修改原始,
#改为调用你好([@ X],[@y],$ Z);出口(0);子你好{    我($ X2,Y2 $,$ Z2)= @_;
    #现在,你去引用数组引用$通过@ $ x2或$ X 2 - &GT X2 [$ i]
    #其中previously您使用@ x2或$ X2 [$ i]
    打印$ _ \\ n表示@ $ X2;
    打印$ _ \\ n;
    打印$ _ \\ n表示@ $ Y2;
    打印$ _ \\ n;
    打印$ Z2 \\ N的;}

How can I pass two arrays and a string to a sub?

Here's what I'm trying to do:

use strict;
use warnings;

my @x = qw(AAAA BBBB CCCC DDDD EEEE);
my @y = qw(1111 2222 3333 4444 5555);

my $z = "hello";

Hello(@x,@y,$z);

exit(0);

sub Hello {

    my (@x,@y,$z) = @_;

    print "$_\n" for @x;
    print "$_\n";
    print "$_\n" for @y;
    print "$_\n";
    print "$z\n";
}


Output:

AAA
BBBB
CCCC
DDDD
EEEE
1111
2222
3333
4444
5555
hello
Use of uninitialized value $_ in concatenation (.) or string at test.pl line 19.

Use of uninitialized value $_ in concatenation (.) or string at test.pl line 21.

Use of uninitialized value $z in concatenation (.) or string at test.pl line 22.


解决方案

You need to pass each of the arrays as a reference, otherwise your @x in the sub will gobble up the ENTIRE array of arguments, leaving @y an empty arraay and $z an undef value.

This happens because the comma operator - in a list context - will turn a @x, @y, $z into a single array consisting of all the elements of @x followed by all elements of @y and then a value of $z; your @x in the sub will gobble up the ENTIRE combined array of arguments, leaving @y an empty array and $z an undef value.

Another possible source of confusion is the fact that you named both variables @x, despite the fact that they are completely independent of each other due to scoping rules. Good practice would be to name them something distinct to avoid having to guess which one you meant to use, e.g. call the subroutine's first array @x2.

Please note that you can pass the array as a reference in one of two ways - the reference to the original array (real pass-by-reference approach) as well as reference to a COPY of the array - which will behave like you wanted your origainal code to behave and pass by value.

use strict; use warnings;

my @x = qw(AAAA BBBB CCCC DDDD EEEE); 
my @y = qw(1111 2222 3333 4444 5555);
my $z = "hello";

Hello(\@x,\@y,$z);

# If you wish to pass a reference of a COPY of the array, 
# so that you can modify it inside the subroutine without modifying the original,
# instead call Hello([@x], [@y], $z);

exit(0);

sub Hello {

    my ($x2,$y2,$z2) = @_;
    # Now, you de-reference array reference $x2 via @$x2 or $x2->[$i]
    # where previously you used @x2 or $x2[$i]
    print "$_\n" for @$x2;
    print "$_\n";
    print "$_\n" for @$y2;
    print "$_\n";
    print "$z2\n";

}

这篇关于我怎么能传递两个数组和字符串到Perl子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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