All pastes #224149 Raw Edit

Anonymous

public text v1 · immutable
#224149 ·published 2006-10-27 07:27 UTC
rendered paste body
#!/usr/bin/perl

# This script should compare 2 files.
# One file contains valid data, the other contains bad records from the first file.
# The script will eventually print good records to a third file, but at the moment prints debugging info.
# The script currently only finds about half of the bad records, 310 of 597 and I don't understand why.

  use strict;
  use warnings;

  my $PARKFILE = shift(@ARGV);
  my $DATAFILE = shift(@ARGV);
  my $OUTFILE = shift(@ARGV);

  my @RECORDS = ();
  my @PARKED = ();
  my @SEARCH = ();

  # Poplulate @RECORDS
  open(DATA,"<","$DATAFILE");
  while (<DATA>) {
    chomp(my $data_line = $_);
    $RECORDS[++$#RECORDS] = $data_line;
  }
  close DATA;

  # Populate @PARKED
  open(PARK,"<","$PARKFILE");
  while (<PARK>) {
    chomp(my $park_line = $_);
    $PARKED[++$#PARKED] = $park_line;
  }
  close PARK;

  # Get rid of the header and trailer records
  shift(@PARKED);
  pop(@PARKED);

  # Get a substr() of the bad record to use in a regex
  for (my $s = 0; $s <= $#PARKED; $s++) {
    $SEARCH[++$#SEARCH] = substr($PARKED[$s], 78, 32);
  }

  open(OUT,">>","$OUTFILE");

  # Find the bad records
  for (my $g = 0; $g <= $#RECORDS; $g++) {
    my $good_rec = $RECORDS[$g];
    for (my $b = 0; $b <= $#SEARCH; $b++) {
      my $bad_rec = $SEARCH[$b];
      if ($good_rec =~ /$bad_rec/) {
        print OUT "MATCH FOUND!\n";
        print OUT "  DATAFILE: $good_rec\n";
        print OUT "    POS IN ARRAY: $g\n";
        print OUT "    LINE IN FILE: ".++$g."\n";
        print OUT "  PARKFILE: $bad_rec\n"; 
        print OUT "    POS IN ARRAY: $b\n";
        print OUT "    LINE IN FILE: ".++$b."\n\n";
      }
    }
  }

  close OUT