[comp.lang.perl] Searching fixed-record-length files

ben@cunixf.cc.columbia.edu (Ben Fried) (11/08/90)

This sounds like something really trivial, but I can't seem to find it
in the manual - I may just not be looking hard enough.

I've got a file containing a database with a fixed record size of 184
characters; there is no record separator.  Same for fields within the
record - they are each a (different) constant size, and are padded with
blanks out to that size.

I was trying to think of a way to slurp in the entire file all at once
(even though it's big - ~8meg), compile the expression to search for,
and then use format to print out the matching records prettily; however,
I'm stuck.  The solutions I come up with are all based around records
and fields all being delimited by some expression, when they're actually
not.

Any suggestions?

Ben
--
Benjamin Fried
ben@cunixf.cc.columbia.edu				rutgers!columbia!ben

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (11/09/90)

In article <1990Nov7.233040.8119@cunixf.cc.columbia.edu> ben@cunixf.cc.columbia.edu (Ben Fried) writes:
: I've got a file containing a database with a fixed record size of 184
: characters; there is no record separator.  Same for fields within the
: record - they are each a (different) constant size, and are padded with
: blanks out to that size.
: 
: I was trying to think of a way to slurp in the entire file all at once
: (even though it's big - ~8meg), compile the expression to search for,
: and then use format to print out the matching records prettily; however,
: I'm stuck.  The solutions I come up with are all based around records
: and fields all being delimited by some expression, when they're actually
: not.

while (read(DB, $_, 184)) {
    ($once, $upon, $a, $time) = unpack('A20 A30 A50 A84', $_);
    ...
}

This calls fread() internally, so you don't have to worry about short
reads except at eof.  The 'A' specifier in the format template will trim
trailing spaces--use 'a' to keep the spaces.

(If this isn't a one-shot program, you probably want to shove the magic
literals into variables that are set at the top of your program, if not
read out of a file.)

Larry