in reply to Unpacking fixed length records

Point 1: This is variable data, not fixed.

Point 2:

How about something like:
open file;
$Header_length = 2;
while(sysread (file, $data, $Header_length)){
$mask = 'xyz';
$body_length = unpack $mask $data;

sysread (file, $data, $body_length);
$mask= 'MNO';
$body=unpack $mask $data;
...
}

Replies are listed 'Best First'.
Re: Re: Unpacking fixed length records
by diotalevi (Canon) on Oct 07, 2002 at 22:10 UTC

    I goofed and called it "Fixed length records" when what would have made more sense was Run Length Encoded (RLE). Oh well. Yes, you are correct in that approach would work - I'm trying to understand how to make it work from an unpack format example. I did recently discover that it's not the format that's bad - something is odd about my data that's breaking the unpack() call. Not that it matters but I can't really do read() and sysread() since I've just grabbed the entire 10K string from a call to /usr/bin/uncompress. See 203336 and 203230 for more on that. I'll probably spend an hour tonight and follow a hexdump of the data to see where unpack is dying. As far as I know there's no way to debug unpack and see where it's falling down. It just either works or it doesn't (and it's damn annoying).

    __SIG__ printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B:: +svref_2object(sub{})->OUTSIDE
      You could separate the parts of the unpack format, making sure to manually consume each part as you go along. It's more work than should be necessary of course - unpack should tell us where it failed, but alas, it doesn't. So you could repeatedly call something like
      my @list = eval { unpack "x$skip $format", $data }; { die "$@" || last }
      making sure that $skip contains the correct value between calls.

      Makeshifts last the longest.