http://www.perlmonks.org?node_id=976892


in reply to Unpacking variable length bit fields

puterboy,
Assuming this data is in a string, I would do something like:
my $skip = 0; while ($skip < length($str)) { my ($f1_len, $f2_len, $f3_len) = unpack("x${skip}aaa", $str); $skip += 3; my $field1 = unpack("x${skip}a$f1_len", $str); $skip += $f1_len; # etc }

This is like using seek which is exactly what you were trying to avoid (iteratively calling pack) but if wrapped in the proper abstraction can be very clean.

Update: After reading BrowserUk's response and re-reading your post, I realize I incorrectly assumed you had multiple records in the string - each one with variable length fields. Please ignore me.

Cheers - L~R