Arrays 为什么我的Perl递归函数永远不会结束?

Arrays 为什么我的Perl递归函数永远不会结束?,arrays,perl,recursion,Arrays,Perl,Recursion,我正在尝试编写以下递归函数。问题是它永远不会结束,我不明白为什么: sub do_smth(@first, @second){ my @tmp_first = @first; $tmp = shift(@tmp_first); if (@tmp_first > 0){ do_smth(@tmp_first, @second); } my @tmp_second = @second; $tmp = shift(@tmp_

我正在尝试编写以下递归函数。问题是它永远不会结束,我不明白为什么:

    sub do_smth(@first, @second){
    my @tmp_first = @first;
    $tmp = shift(@tmp_first);
    if (@tmp_first > 0){
        do_smth(@tmp_first, @second);
    }
    my @tmp_second = @second;
    $tmp = shift(@tmp_second);
    if (@tmp_second > 0){
        do_smth(@first, @tmp_second);
    }

}

您首先移动未定义的标量$tmp_,然后移动$tmp_


我没有再看了

此代码甚至不编译。如果没有警告和严格限制,您将出现以下错误:

Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 5, near "$tmp_first)"
Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 10, near "$tmp_second)"
Execution aborted due to compilation errors.
并提出警告和严格要求:

Illegal character in prototype for main::do_smth : @first,@second at so.pl line 4.
Global symbol "@first" requires explicit package name at so.pl line 5.
Global symbol "$tmp" requires explicit package name at so.pl line 6.
Global symbol "$tmp_first" requires explicit package name at so.pl line 6.
Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 6, near "$tmp_first)"
Global symbol "@second" requires explicit package name at so.pl line 8.
Global symbol "@second" requires explicit package name at so.pl line 10.
Global symbol "$tmp" requires explicit package name at so.pl line 11.
Global symbol "$tmp_second" requires explicit package name at so.pl line 11.
Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 11, near "$tmp_second)"
Global symbol "@first" requires explicit package name at so.pl line 13.
Execution aborted due to compilation errors.
我不知道您想做什么,但以下是您的代码,语法正确:

use warnings;
use strict;

sub do_smth (\@\@);  # predeclaration needed since the prototyped sub
                     # is called recursively
sub do_smth (\@\@) {
    my ($first, $second) = @_;
    my @tmp_first = @$first;
    my $tmp = shift(@tmp_first);
    if (@tmp_first > 0){
        do_smth(@tmp_first, @$second);
    }
    my @tmp_second = @$second;
    $tmp = shift(@tmp_second);
    if (@tmp_second > 0){
        do_smth(@$first, @tmp_second);
    }
}

首先转移$tmp_;是语法错误。我们不能移动标量。应该是shift@tmp_first;如果需要函数签名,则需要安装或运行。它们不是香草Perl 5.0中的内置特性,请严格使用;使用警告;将给您以下信息:要移位的arg 1的类型必须是数组而不是私有变量。使用诊断;可能会提供更多信息。这应该做什么,为什么需要使用递归?这不是答案,应该是注释。@Daenyth=>这不是它要做的,原型将数组引用上下文强加于2个必需参数上。这样说来,你称之为“一、二”而不是“一、二”,其实你是对的。我只是太习惯于看到它被认为是用于论点签名的人滥用了。下次我会尽量把我的膝盖抽搐降到最低;