Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

From Re^4: how to unpack a C struct with length array preceding data array (emphases added):

I do agree with ig though, you should avoid [ig's] solution if possible, it's hard to read, and it would be difficult to expand it for a string of three parts or more.

In the spirit of ig's ingenious reply and of Anonymonk's excellent explication, here's a version that unpacks four data items in a single unpack statement (the initial  s4 is optional if you don't care about all the length info):

>perl -wMstrict -MData::Dump -le "my $str = qq{\x05\x00\x08\x00\x05\x00\x06\x00Just Another pack Hacker +}; ;; my $template = q{ ( s4 @0 x[s0] s @0 s0 x[s4] /a @0 x[s1] s @0 s1 x[s3] /x /a @0 x[s2] s @0 s2 x[s2] /x /x /a @0 x[s3] s @0 s3 x[s1] /x /x /x /a ) }; ;; my @ra = unpack $template, $str; dd \@ra; " [5, 8, 5, 6, "Just ", "Another ", "pack ", "Hacker"]

As you can see, this is pretty orthogonal and easily generalized to any number of data items. (I can post the code to generate the unpack template for any array 'type' and any number of data items on my scratch pad if anyone's interested.) It also involves a lot of shuttling back and forth to pick up and use the various unpack elements, and this has the odor of wasted motion. One can also see how such a template could grow rather unwieldy if one were dealing with 400 or 4000 data items rather than just four.

All the admonitions remain in force: this is a trick you should only try at home, never in any public, much less any occupational, venue.

I suspect that a much more concise version of this exists. Ideally, I would like to wind up with a template looking something like
    ((clever unpack template)n)
where n is the number of data items to be unpacked; i.e., to roll up what is effectively an unrolled loop. Unfortunately, I just can't see my way clear to this solution. I'll keep trying...


In reply to Re: how to unpack a C struct with length array preceding data array by AnomalousMonk
in thread how to unpack a C struct with length array preceding data array by johnlumby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (11)
    As of 2014-08-22 14:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (158 votes), past polls