这段代码看起来真的一点也不像 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);
至少,这将编译。但我不知道它是否有效,因为我不知道它应该做什么。