Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Unpacking variable length bit fields

by Limbic~Region (Chancellor)
on Jun 18, 2012 at 20:36 UTC ( #976892=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://976892]
help
Chatterbox?
[Your Mother]: Regarding last unicorns… http://i.imgur. com/T3L9e.jpg
[marto]: also unicorn power
LanX still prefers Jesus vs Krishna
[LanX]: found it :)
LanX apologizing , please s/Krishna/Ganesha/
zentara ...oO speaking of unicorns and Krishna :-)
[zentara]: Jesus would never bite Ganeesh ! Jesus didn't fight or eat meat

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2017-03-30 16:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (361 votes). Check out past polls.