Path: utzoo!attcan!uunet!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Searching fixed-record-length files Message-ID: <10304@jpl-devvax.JPL.NASA.GOV> Date: 9 Nov 90 02:01:46 GMT References: <1990Nov7.233040.8119@cunixf.cc.columbia.edu> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 27 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