In general, no, it does not hold. For the example I extracted only the part that I had problems with. In real life there is more structure before and after the example structure.
I also hoped to learn how flexible and concise the packing templates can be, but of course I need to digest the perlpacktut documentation now (which I did not knew existed).
Thanks for good advice.
Edit: Here is a more general example with three arrays of different sized types.
Now it is
byte: number of members in each of the following arrays
array of bytes
array of unsigned shorts
array of longs
I updated the script and added the one step solution (using the excellent explanation of
Eily):
use strict;
use warnings;
my $testinput = pack('C/a* a* a*',
(pack 'C*', 1, 2),
(pack 'v*', 3, 4),
(pack 'l*', 5, 6));
print join(',', unpack('C/C* v2 l2', $testinput)), "\n";
# gives "1,2,3,4,5,6" which is ok,
# but has the repeat factors for 'v' hardcoded
my $repeat = unpack('C', $testinput);
print join(',', unpack("C/C* v$repeat l$repeat", $testinput)), "\n";
# gives "1,2,3,4,5,6" which is ok, but uses two steps
print join(',', unpack('C/C* @0 CXC /(x[C]) xX /v @0 CXC /((x[C])(x[v]
+)) xX /l', $testinput)), "\n";
# gives "1,2,3,4,5,6" uses one step, but is a bit complex
1;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.