将变量传递给Perl子例程

前端之家收集整理的这篇文章主要介绍了将变量传递给Perl子例程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将以下变量传递给子程序mySubroutine,$name,$age,然后是这个多维数组:
$name = "jennifer";
$age = 100;

$list[0][0] = "TEST NAME 2";
$list[0][1] = "TEST GROUP 2";
$[0][2] = 10;

$[1][0] = "TEST NAME 2";
$[1][1] = "TEST GROUP 2";
$[1][2] = 2;

子程序:

sub mySubroutine
{

}

我已经尝试了$_ [0]和@_,但我似乎没有得到正确传递给子例程的变量.

解决方法

有几种方法可以实现(像Perl中的大多数事情).我个人这样做:
sub mySubroutine
{
    # Get passed arguments
    my ($name,$refList) = @_;

    # Get the array from the reference
    my @list = @{$refList};
    # Good to go
}

# You need to pass @list as reference,so you 
# put \@list,which is the reference to the array
mySubroutine($name,\@list);
原文链接:https://www.f2er.com/Perl/171562.html

猜你在找的Perl相关文章