Front page | perl.beginners |
Postings from December 2002
'foreach' and error diagnosis
Thread Next
From:
Al Lukaszewski
Date:
December 20, 2002 08:37
Subject:
'foreach' and error diagnosis
Message ID:
20021220163155.A10729@localhost.localdomain
Greetings,
I'm writing a script that is intended to read in a comma-delimited
file called "qa.db" line-by-line. Each line is split into fields and
the first two fields are evaluated. The first field is a line
reference; the second is a lemma. If the line reference is the same
as the previous entry, the lemma should be appended to the *beginning*
of the output line ("qa.txt"). If the line reference is different,
two blank lines and the new line reference should be appended to the
output file.
Among the problems I am encountering, one is a conceptual one: how do
I get Perl to write the lemmas right to left (i.e., unshift) but not
append to the very beginning of the file.
I also am getting some error messages. When I run the code pasted
below, I get the following:
*[OUTPUT]*
syntax error at ./chgr.pl line 34, near "$line("
syntax error at ./chgr.pl line 46, near ") # If the evaluation scalar and control scalar are not equal, do the following:
{"
Global symbol "@newQA" requires explicit package name at ./chgr.pl line 47.
Global symbol "@newQA" requires explicit package name at ./chgr.pl line 47.
syntax error at ./chgr.pl line 50, near "}"
Execution of ./chgr.pl aborted due to compilation errors.
Any help would be appreciated very much.
Yours,
Al Lukaszewski
*[PROGRAM]*
#!/usr/bin/perl
#
# Read in the data file
#
use strict;
use warnings;
my $file;
$file = '4Q246.db' ; # Name the file
open(INFO, "$file" ) ; # Open the file
my @db;
@db = <INFO> ; # Read it into an array
close(INFO) ; # Close the file
# Initialize scalars
my $line; # = scalar by which program steps through data
my $fieldRef; # = holding scalar for line reference field
my $fieldForm; # = holding scalar for the lemma field
my $fieldMorph; # = holding scalar for the parsing field
my $fieldSynt; # = holding scalar for the syntax field
my $fieldLex; # = holding scalar for the lexical information field
my $newQA = ""; # = holding scalar for the output, the contents of this scalar will be the compiled texts and will be written to the output file
my @field;
my $fieldEval;
my $fieldCtrl = "null" # Preset control variable to 'zero'
foreach $line(@db) # Assign the contents of @db to $line one line at time for evaluation.
{
chomp ($line);
@field = split /,/, $line;
($fieldRef, $fieldForm, $fieldMorph, $fieldSynt, $fieldLex) = ($field[0], $field[1], $field[2], $field[3], $field[4]);
$fieldEval = $fieldRef # Assign current line reference to evaluation scalar
unless ($fieldEval eq $fieldCtrl) # If the evaluation scalar and control scalar are not equal, do the following:
{
@newQA = @newQA . "\n\n" . $fieldRef; # First append two new lines to the output scalar; also, append the new line reference for the fields to come
$fieldCtrl = $fieldRef; # Assign the new line reference to the control scalar
}
@newQA= unshift @newQA, " ";
@newQA = unshift @newQA, $fieldForm;
next $line;
}
my $fileOut;
my @corpus = @newQA;
open $corpus, ">>qa.txt";
close $corpus; # Close the file
Thread Next
-
'foreach' and error diagnosis
by Al Lukaszewski