-4

我尝试在 Perl 中调用子例程,但我收到此错误“主原型格式错误”。我有一个子程序比较,我必须将两个整数传递给它。

#!/usr/bin/perl
@ListA=(1,2,3);
@ListB=(2,3,4);
@ListResult;
#AND

sub Compare($p1,$p2){                   
    if($p1 > sizeof(ListA) or $p2 > sizeof(ListB))
        { 
                  return;}                          

    if(ListA($p1) = ListB($p2)){                    
        push (@ListResult, ListA($p1)); 
        Compare($p1+1,$p2+1);           
        return;                         
    }
    if(ListA($p1) > ListB($p2)){        
        Compare($p1,$p2+1);             
        return;                         
    }
    else {                              
        Compare($p1+1,$p2);             
        return;                         
         }
    return;                             
}

Compare(1,1);

请帮助我并解释如何纠正这个程序。

4

2 回答 2

5

这是错误的:

sub Compare($p1,$p2){      

Perl 不会那样做。这将是您报告的错误的来源。(从技术上讲,它是原型设计,这是您不应该使用的 perl 功能,因为它不会像您想的那样做)。

您可能想要:

sub Compare{      
    my ( $p1, $p2 ) = @_; 
     #etc.

另外:打开use strict;use warnings;。一开始它们很烦人,但在编程陷阱方面它们确实做得很好

喜欢:

sizeof(ListA) 

无效,因为ListA是一个裸词。或者一个字符串,也许。但它不是你的@ListA. 这比你想象的要简单——标量上下文中的列表返回它的长度。所以你可以简单地做$p1 > @ListA,它会起作用。

和:

 if(ListA($p1) = ListB($p2)){     

是:

  • 再次使用裸词(所以不引用@ListA)。
  • ()在应该使用的时候使用[]。(访问数组索引是$arrayname[0]
  • 使用=which 是一个分配,而不是==(或eq用于字符串)这是一个比较。
于 2015-10-23T13:01:27.543 回答
4

这段代码看起来真的一点也不像 Perl。

#!/usr/bin/perl

# You should always have "use strict" and "use warnings"
# at the top of your Perl programs.

# When you have "use strict", then you need to declare your
# variables, most commonly using "my".
@ListA=(1,2,3);
@ListB=(2,3,4);
@ListResult;
#AND

# Perl subroutines aren't defined like this
sub Compare($p1,$p2){
    # You need to use the @ at the start of the array name
    # whenever you are referring to the whole array.
    # And Perl doesn't have a "sizeof" function.
    # Also, your indentation style is *horrible* here :-)
    if($p1 > sizeof(ListA) or $p2 > sizeof(ListB))
        { 
                  return;}                          

    # I think you're trying to look up individual array elements
    # here. That's $array[$index], not array(index).
    # And in Perl, '=' is always an assignment operator. Here,
    # You probably want '==' which is a comparison operator.
    if(ListA($p1) = ListB($p2)){                    
        push (@ListResult, ListA($p1)); 
        Compare($p1+1,$p2+1);           
        return;                         
    }
    if(ListA($p1) > ListB($p2)){        
        Compare($p1,$p2+1);             
        return;                         
    }
    else {                              
        Compare($p1+1,$p2);             
        return;                         
         }
    return;                             
}

Compare(1,1);

你的程序应该是这样的:

#!/usr/bin/perl

use strict;
use warnings;

# I've changed the names of these variables, because in Perl
# arrays and lists are different things. These are arrays.
my @ArrayA = (1,2,3);
my @ArrayB = (2,3,4);
my @ArrayResult;
#AND

sub Compare {
  my ($p1, $p2) = @_;

  return if $p1 > @ArrayA or $p2 > @ArrayB;         

  if ($ArrayA[$p1] == $ArrayB[$p2]) {
    push (@ArrayResult, $ArrayA[$p1]);
    Compare($p1+1, $p2+1);
    return;
  }

  if ($ArrayA[$p1] > $ArrayB[$p2]){
    Compare($p1, $p2+1);
    return;
  } else {                              
    Compare($p1+1, $p2);
    return;                         
  }
  return;                             
}

Compare(1,1);

至少,这将编译。但我不知道它是否有效,因为我不知道它应该做什么。

于 2015-10-23T13:40:20.770 回答