On Sep 13, 4:23 pm, [EMAIL PROTECTED] (Tim McGeary) wrote:
> Hi all,
>
> I'm trying to read in two file sets of library records, compare a regex
> that I find in different lines of each set of records, and then copy one
> whole line over to the other when a match is found.
>
> To do this, I am trying to use a 3-dimensional array:
> [fileset][record][line]
>
> I think that I am reading and pushing the arrays onto the other arrays
> correctly, but when I'm traversing the arrays to do the conditionals, I
> am getting uninitialized value warnings, which seems like I'm either not
> traversing correctly OR I'm not really pushing the data onto the arrays
> properly.

Having looked at your code I very much doubt you are putting the right
data into the arrays.

Without seeing the input file format it's kinda hard to judge by you
seem to be clearing @record too rarely (it just keeps growing) and
@line too often (it could nevet have more than one element).

I suggest you dump it with Data::Dumper.

> This is my whole script below.  Does anyone see what I'm doing wrong?

By the way, you are suffering from premature declaration.

> use strict;
> use warnings;
>
> our $marchead1 = '*** DOCUMENT BOUNDARY ***';
> our $marchead2 = 'FORM=MARC';
>
> sub read_records {      # read in whole MARC record and push it to @marc
>
>     my @marc;
>     my @record;
>     my @line;
>     open FILE, $_[0] or die qq(Cannot open file $_[0]\n);

As a beginner there's little point learning the legacy forms of
open().

You should put the error in your error message.

     open my $file, '<', $_[0] or die qq(Cannot open file $_[0]: $!
\n);


>     print "Opening file $_[0]...\n";
>     my $index = -1;
>     while (<FILE>) {
>        chomp;
>        #print "Reading in $_\n";
>        if ($_ eq $marchead1) {
>           if ($index >= 0) {
>              push @marc, [ @record ];
>              print "Pushing record onto marc array...\n";
>              print "Next record....\n\n";
>           }
>           $index++;

All that stuff with $index seems to be an effort to avoid pushing an
emply record into @marc.

If that's really all you are trying to do then you could make it
clearer.

              push @marc, [ @record ] if @record;

You print "Next record....\n\n" but neither clear @record nor to a
next.

>        }
>        elsif ($_ eq $marchead2) { next; }
>        else {
>           @line = ();
>           my $field = substr($_, 0, 5);
>           my $data = substr($_, 5);
>           push @line, [ $field, $data ];
>           #print "Splitting line $field\t$data\n";
>           next;
>        }
>        push @record, [ @line ];
>     }
>     print "Closing file $_[0]...\n";
>     close FILE;
>     return @marc;
>
> }

No, that's really all totally confused.

At a random shot-in-the-dark you were trying to do something like the
following...

(Since you didn't provide sample data this is just an untested wild
guess).

sub read_records {    # read in whole MARC record and push it to @marc
    my ($filename) = @_;
    my (@marc,@record,@line);
    open my $file, '<', $filename or die qq(Cannot open file
$filename: $!\n);
    print "Opening file $filename...\n";
    local *_;
    while (<$file>) {
        chomp;
        if ($_ eq $marchead1) {
            push @record, [ @line ] if @line;
            push @marc, [ @record ] if @record;
            @line = @record = ();
        } elsif ($_ eq $marchead2) {
            push @record, [ @line ] if @line;
            @line = ();
        } else {
            my $field = substr($_, 0, 5);
            my $data = substr($_, 5);
            push @line, [ $field, $data ];
        }
    }

    push @record, [ @line ] if @line;
    push @marc, [ @record ] if @record;

    print "Closing file $filename...\n";
    return @marc;
}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to