开发者

Returning 2 arrays from subroutine depending on stop list

开发者 https://www.devze.com 2023-03-16 09:22 出处:网络
This has been moved to a test case here. RE-DONE: I want to return arrays (must be references) from 2 subroutines, however the regex used as a conditional statement isn\'t working as I\'d hoped. I\'ve

This has been moved to a test case here.

RE-DONE:

I want to return arrays (must be references) from 2 subroutines, however the regex used as a conditional statement isn't working as I'd hoped. I've tried doing it with one, but I figure this will be easier.

To be clear, my goal is to have an array of matches sorted (@all_matches), and then add on another array (@all_pronoun_matches) sorted the same way but added at the end.

This is the @pronoun_matches subroutine:

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);

The @matches has a very similar subroutine except this:

foreach my $pronoun (@stopListNoun) #开发者_JS百科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;

This is called by:

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);

To send to the print section after being sorted using hashes, I'd like to use:

@all_matches = (@all_matches, @all_pronoun_matches); However, @all_pronoun_matches has 0 matches (or they are being filtered somewhere).

Question

Why does @all_pronoun_matches have uninitialized values in it?? After some testing, I've found that the match never gets passed the conditional statement, but it's the same as the one in the @matches subroutine!


Originally, I had just wanted to remove the pronouns and it worked fine, so I know the condition works:

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

I've tried using an if-else in the foreach and combining the subroutines, but then all the matches (including pronouns) went into @all_matches despite being called correctly (this method was posted here before).

Let me know if anything is unclear about my intent or the problem.


@all_matches = @matches, @all_pronoun_matches;

should be

@all_matches = ( @matches, @all_pronoun_matches );

, has lower precedence than =

If you had warnings enabled, you would have gotten a Useless use of a variable in void context warning alerting you that @all_pronoun_matches didn't become part of the assignment.

0

精彩评论

暂无评论...
验证码 换一张
取 消