根据停止列表从子例程返回2个数组

时间:2011-06-30 22:11:31

标签: perl

这已移至测试用例here

RE-DONE:

我想从2个子程序返回数组(必须是引用),但是用作条件语句的正则表达式并不像我希望的那样工作。我试过用一个,但我觉得这会更容易。

要明确的是,我的目标是将一系列匹配排序(@all_matches),然后添加另一个数组(@all_pronoun_matches),以相同的方式排序,但在结尾添加

这是@pronoun_matches子例程:

my ($line, $verbform, $chapternumber, $sentencenumber, $sentence) = @_;
my @matches;
my @pronoun_matches;
return unless ($line =~ /(\w+)\((\w+)\-\d+\,\s(\w+)\-\d+\)/); #2nd repeat check     
$grammar_relation = $1;
$argument1 = $2;
$argument2 = $3;

return if (($argument1 =~ /^$argument2/i)||($argument2 =~ /^$argument1/i));    

foreach my $pronoun (@stopListNoun)
    {
    if ((lc $pronoun eq lc $argument1) || (lc $pronoun eq lc $argument2)) 
        {
        push (@pronoun_matches, $chapternumber, $sentencenumber, $sentence, $grammar_relation, $argument2, $argument1) if ($argument2 =~ /$verbform/i);
        push (@pronoun_matches, $chapternumber, $sentencenumber, $sentence, $grammar_relation, $argument1, $argument2) if ($argument1 =~ /$verbform/i);
        }
    else
        {return}
    }

return (\@pronoun_matches);

@matches有一个非常相似的子程序,除了这个:

foreach my $pronoun (@stopListNoun) #Just a list of words
    {
    return if ((lc $pronoun eq lc $argument1) || (lc $pronoun eq lc $argument2));
    }

    push (@matches, $chapternumber, $sentencenumber, $sentence, $grammar_relation, $argument2, $argument1) if ($argument2 =~ /$verbform/i); ##USED TO BE 'eq', but that prevented protective from showing
    push (@matches, $chapternumber, $sentencenumber, $sentence, $grammar_relation, $argument1, $argument2) if ($argument1 =~ /$verbform/i);

    return \@matches;

这称为:

my $matches;
my $pronoun_matches;
$matches = &dependency_checks($lines[$l], $verbform, $chapternumber, $sentencenumber, $sentence);
$pronoun_matches = &pronoun_dependency_checks($lines[$l], $verbform, $chapternumber, $sentencenumber, $sentence);
push @all_matches, $matches if ($matches);
push @all_pronoun_matches, $pronoun_matches if ($pronoun_matches);

要在使用哈希值进行排序后发送到打印部分,我想使用:

@all_matches = (@all_matches, @all_pronoun_matches);但是,@all_pronoun_matches有0个匹配(或者它们正在某处进行过滤)。

问题

为什么@all_pronoun_matches中包含未初始化的值? 经过一些测试,我发现匹配永远不会通过条件语句,但它与@matches子例程中的匹配相同!


最初,我只想删除代词并且工作正常,所以我知道条件有效:

foreach my $pronoun (@stopListNoun)
        {
        return if ((lc $pronoun eq lc $argument1) || (lc $pronoun eq lc $argument2));
        }

我尝试在foreach中使用if-else并组合子程序,但是所有匹配(包括代词)都进入@all_matches尽管被正确调用(此方法之前已在此处发布)。< / p>

如果我的意图或问题不明确,请告诉我。

1 个答案:

答案 0 :(得分:6)

@all_matches = @matches, @all_pronoun_matches;

应该是

@all_matches = ( @matches, @all_pronoun_matches );

,优先级低于=

如果您启用了警告,则会收到Useless use of a variable in void context警告,提醒您@all_pronoun_matches未成为转让的一部分。