jonc has asked for the wisdom of the Perl Monks concerning the following question:
Hello PerlMonks! I'm new here, and am a beginner programmer (Perl is first language). Let me know if this post is up to code (haha...sorry)
As shown in subject, I am getting that error(see comment ERROR HERE at last line of code) when I run the code below. It is fixed if I uncomment the if(defined $item).
However, I think it is better to fix the problem instead of ignoring it. It prints "chpt31_4" four times and then gives the error message. Hopefully you can help debug this or better, explain it:
Here is the code:
#!/usr/bin/perl use strict; use warnings FATAL => "all"; #use diagnostics; require 'verbTenseChanger.pl'; ## -JUST FOR TESTING- ## my $chapter_section = "chpt"."31_4"; my $search_key = "move"; my $category_id = "all"; # --- Files --- # open(my $parse_corpus, '<', "parsed${chapter_section}.txt") or die $!; # --- Different forms of the Searchword --- # my @temp_changeverbforms = map changeVerbForm( $search_key, 0, $_ ), 1 +..4; my @verbforms; push (@verbforms, $search_key);# Watch extra for loop foreach my $temp_changeverbforms (@temp_changeverbforms) { push (@verbforms, $temp_changeverbforms) unless ($temp_changeverbf +orms eq ""); } # --- Variables for Searching/working with parser and tagger --- # my @lines = <$parse_corpus>; my $chapternumber_value; my $sentencenumber_value; my @all_matches; my $sentence; my $grammar_relation; my $argument1; my $argument2; foreach my $line (@lines) { print "$line\n\n"; ##TEST ONLY## foreach my $verbform (@verbforms) { my $one_match_ref = []; #Chpt. num (Could check %seens): if ($line =~ /^Parsing file: (chpt\d+_\d+).txt/) { $one_match_ref->[0] = $1 ; ##Only once in file } if ($line =~ /\b$verbform\b/i) { #Sent. num: #SentSentences: if ($line =~ /^Parsing \[(sent. \d+) len. \d+\]: \[(.+)\] +/) { $one_match_ref->[1] = $1; ##Multiple in file $one_match_ref->[2] = $2; $one_match_ref->[2] =~ s/,//g; } #Dependencies: if ($category_id eq "all") { if ($line =~ /subj\w*\(|obj\w*\(|prep\w*\(|xcomp\w*\(| +agent\w*\(|purpcl\w*\(|conj_and\w*\(/) { if ($line =~ /(\w+)\((\w+)\-\d+\,\s(\w+)\-\d+\)/) +{ $one_match_ref->[3] = $1; $one_match_ref->[4] = $2; $one_match_ref->[5] = $3; } } } } # add the reference of match into array. push (@all_matches, $one_match_ref); } } # walk through all the matches. foreach my $array_ref (@all_matches) { foreach my $item (@{$array_ref}) { print "$item\n";# if (defined($item)); ##ERROR HERE!!!! } }
I also have an 'array of hash' version, if it would be better. Hopefully the fix can be used there as well
Not sure if this is needed, but here is the format of the file being passed in:
Parsing [sent. 34 len. 34]: [The, form, of, its, skeleton, and, body, cavities, strongly, influences, the, degree, to, which, an, animal, can, control, and, change, its, shape, ,, and, thus, the, complexity, of, the, movements, it, can, perform, .]
nsubj(evolved-12, animals-11)
ccomp(suggests-9, evolved-12)
prep_from(evolved-12, colonies-14)
The purpose of this code is to search the file for chapternumber(ie. chpt31_4, sentence number(ie. sent. 34), sentence itself, grammar relation (ie. nsubj), arg1 (ie. evolved) and arg2 (ie. animals).
I get all these elements into an array, and they constitute 1 match. I need an array of all these match arrays
|
|---|