Hi folks,

Be gentle, this is my very first Perl program, and it is basically a batch
file processing task, nothing fancy. 
I know it is not the most efficient, but I want to keep it as simple and
clear as possible. Once I get it working, I can make it tighter.

I am getting 2 or 3 errors, though they may all be traced back to 1:

syntax error at /script/path/and/name.pl line 42, near "until eof"
syntax error at /script/path/and/name.pl line 45, near "if $found "
Global symbol "%found" requires explicit package name at
/script/path/and/name.pl line 45.
Execution of /script/path/and/name.pl aborted due to compilation errors.


I can not find anywhere in any of the Perl books I have, ( PeachPit, Blue
Llama and the O'Reilly CD set), that gives a good clear example of how to
use 'eof' or 'until'.
I also seem to be having a problem with one of my variables, though they are
all declared the same way. Must be it's use.

The files are too large to load into arrays, and I must process only a
single set of records from the new input file at a time. 

I can not process the whole new input file at once, since I need to put an
entire set of records from the new input file out to the 
output file if any part of that set has changed from what is in the old file
or is new. So I need to work on only one set of records at a time.

A set of records consists of 3 or more, as detailed below. So I need to read
in 3, or more, and compare each to the entire old input file in order 
to determine if the new input set of records is to be written out or
discarded..

Any clues?


Thanks, Tom




#!/usr/bin/perl -w
use strict;

#-------------------------- declare variables
-------------------------------

my $done = 0;
my $found = 0;
my $new_rec;
my $old_rec;
my $old_rec_key;
my @new_recs = (); # will contain anywhere from 3 to ??
my $ary_ctr = 0;
my $ary_item;
my $ary_rec_key;

#-------------------------- set up processing
-------------------------------

open (NEWFILE,"</new_datafile.dat") || Error('open','file NEWFILE');

open (PROCESSFILE,">>/data_to_output.dat") || Error('open','file
PROCESSFILE');

$new_rec = NEWFILE->getline();          # get first line from file to start
                                        # processing - first line will
always be
                                        # record type 1 - if eof/empty file,
                                        # will cause program to exit on
start
                                        # of following loop
#file/record structure is hierarchical, 3 record types in parent-child
#relationship, with varying numbers of dependants:
#rec1
# rec2
#  rec3
#rec1
# rec2                                        
#  rec3
# rec2
#  rec3
#rec1
#etc.......

#------------------------------  main loop -------------------------------

until eof NEWFILE  {
        &Fill_Array;
        &Comp_Old_File;
        if $found {
                &Print_Recs;
        }
}

#------------------------------- subroutines -----------------------------

#--------------------- fill array sub ------------------------------------
#--------------------- should always start with first rec in set ---------
#--------------------- waiting as type 1 rec -----------------------------
#--------------------- continue filling array until type 1 rec for next --
#--------------------- set read ------------------------------------

sub Fill_Array {                        # assume first rec is read and is
type 1
        @new_recs = ();                 # empty the array
        $ary_ctr = 0;                   # reset array subscript
        $done = 0;                      # set done to not done
        while not $done {
                @new_recs[$ary_ctr] = $new_rec; # first rec will always be 1
                $new_rec = $NEWFILE->getline(); # get next rec, may be type
1 for next set
                $ary_ctr++;                     # increment subscript
                if (substr($new_rec, 21, 1) = '1') { # check for next type 1
                        $done = 1;
                }
        }
}


#------------------- compare new file recs to old file recs sub ----------
#------------------- if any of the new recs are not found or do not ------
#------------------- match the old recs, entire set of records goes to ---
#------------------- new file
#------------------- 1400 byte records, first 40 bytes identifies rec ----
#------------------- files may be up to 150 mb each, ---------------------
#------------------- too long for grep, too complicated for awk ----------
#------------------- can not load entirely into arrays -------------------

sub Comp_Old_File {
        foreach $ary_item (@new_recs) {
        $ary_rec_key = substr($ary_item, 0 40);
        open (OLDFILE,"</old_datafile.dat") || Error('open','file OLDFILE');
                while (<OLDFILE>) {
                        $old_rec = $_;
                        $old_rec_key = substr($old_rec, 0, 40);
                        if ( $ary_rec_key eq $old_rec_key ) {
                                print "keys match \n";
                                if ($ary_item eq $old_rec ) {
                                        print "recs match \n";
                                        $found = 1;
                                } else {
                                        print "recs do not match \n";
                                        $found = 0;
                                        last;
                                  }
                        } 
                } 
                print "closing old file \n";
                close (OLDFILE) || Error('close','file OLDFILE');
        } 
} 

#------------------ print out changed or new recs to new file sub
------------


sub Print_Recs {
        foreach $ary_item (@new_recs) {
                print PROCESSFILE $ary_item;
        }
}

#------------------ end processing routines
---------------------------------


print "closing new file \n";
close (NEWFILE) || Error('close','file NEWFILE');
print "closing PROCESS file \n";
close (PROCESSFILE) || Error('close','file PROCESSFILE');

#------------------ error routine(s)
----------------------------------------

sub Error {
print "OLDFILE = old_datafile.dat \n";
print "NEWFILE = new_datafile.dat \n";
print "cannot $_[0] $_[1]:$! \n";
exit;
}

#------------------ end program --------------------------------------------

Thanks,
Tom Wright
201.930.7517


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to