Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi. i'm writing a music database at work, just to see whether or not it will work. each week there are entries and deletions from the catalogue that come in the form of an update file. the update file has different types of records that will contain different information. the file is fixed length and delimited. the first 2 characters are the record type. a typical update file would look like this...

1-2 3-10 12-18

00 date headernumber

1-2 3-5 5~

01 label artist~title~style etc

the 00 record contains the header number which identifies what cd it is. a 00 record is ALWAYS followed by a different record type who's information would pertain to the preceding 00 record UNLESS the item is to be deleted. if the item is to be deleted it would have a 00 record on its own (which would then be followed by another 00 record which would contain information about a different cd, unless it was being deleted) and so on and so forth.

so what i think i need to do is read 2 lines at a time and if there are 2 00 records in a row, the first one is to be deleted. i'm not really sure how to go about reading in 2 lines at a time. does anyone have any ideas, or a better solution?

thanx in advance

  • Comment on fixed length records, reading lines and catalogue updates

Replies are listed 'Best First'.
Re: fixed length records, reading lines and catalogue updates
by BrowserUk (Patriarch) on Jun 19, 2002 at 10:12 UTC

    This (untested) code might get you started.

    open( FILE, "<filename") or die "Can't open file: $!\n"; my @lines = <FILE>; close( FILE) or die "Can't close file: $!\n"; my $LastWasZero = 0; foreach my $line (@lines) { chomp $line; my ($recType, $rest) = unpack "A2A*", $line; if ($recType = "00") { if ($lastWasZero) { # two consequative 00 recs my ($date,$headerNumber) = unpack "A10A20", $lastWasZero; #do delete processing on $headernumber $date... } my ($date,$headerNumber) = unpack "A10A20", $rest; $lastWasZero = $rest; next; } else $lastWasZero = ""; if ($recType eq "01" ) { my ($field1, $field2,....) = unpack "...", $rest; # process 01 records next; } if ($recType eq "02" ) { my ($field1, $field2,....) = unpack "...", $rest; # process 02 records next; } .... }

    Coded for clarity rather than efficiency

    Corrected cut&paste error & brain fade error