Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: unpack - array takes too many bytes

by jjap (Monk)
on Apr 15, 2011 at 04:58 UTC ( #899559=note: print w/ replies, xml ) Need Help??


in reply to Re: unpack - array takes too many bytes
in thread unpack - array takes too many bytes

Wonderful!

I guess my misunderstanding was to expected the digit in C2 to limit the assignment to 2 bytes, which it clearly does not.

Actually replacing C2 by C1 in your code like so:
(@two[0..1], @four[0..3], $one) = unpack "C1 C4 C", $data; print "@two | @four | $one"; # 65 66 | 67 68 69 70 |
And warns for uninitialized likely undef. I am really missing the purpose of what the digit does!?


Comment on Re^2: unpack - array takes too many bytes
Download Code
Re^3: unpack - array takes too many bytes
by Eliya (Vicar) on Apr 15, 2011 at 05:36 UTC
    I am really missing the purpose of what the digit does!?

    Essentially, the unpack just returns a list of values that you have to assign in some meaningful way.

    The digits in the template are just telling unpack how to parse the input data. No provisions are made to assign "sub-lists" to separate arrays, or some such.  In other words, in the example "C2 C4 C" has the same effect as saying "C7".  But note that in other cases, such as "A2 A4 A", where A2 etc. would return a single element, there would a difference to saying "A7".

      The difference is that "C" is a fixed length format (one byte) while "A" is a variable length format so a number after "C" specifies how many Cs while a number after "A" specifies the length of one A.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://899559]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2014-11-27 17:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (186 votes), past polls