in reply to Reading nested records in binary data

How do you know that substr is pretty inefficient?

Anyway, I'd use unpack for this, as it deals pretty well with extracting fixed-length stuff from strings:

my ($header, @items) = unpack "a300(a18)250", $buff;

Replies are listed 'Best First'.
Re^2: Reading nested records in binary data
by Anonymous Monk on Mar 09, 2006 at 23:38 UTC
    I think I answered my own question. I think it should be -
    my ($header, @items) = unpack "a300" . "a18" x 250, $buff;
Re^2: Reading nested records in binary data
by Anonymous Monk on Mar 09, 2006 at 23:25 UTC
    Well, I tried that code for unpack, and I get an error -
     Invalid type in unpack: '(' at code.pl line 98
    . Is that the correct syntax for the template?

      Older versions of perl don't understand ( in pack/unpack templates. I believe that template syntax was introduced in perl 5.8.

Re^2: Reading nested records in binary data
by Anonymous Monk on Mar 09, 2006 at 23:08 UTC
    Thanks, I'll try unpack. Re: substr, since these are fixed length records, it takes less effort to use unpack, like you have in your code, as opposed to using substr multiple times. This is, of course, based on intuition - I know how efficient code actually is depends on other factors too. But I like less code :)