Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

ig's reply was quite instructive, I had some trouble understanding it, but it was worth the trouble.

The (..) could be omitted, but since @ is a position "counted from the start of the innermost ()-group", by putting them there you can reuse this template as is in a bigger structure. (see pack)

W is "An unsigned char value " so 'W W' takes the two first chars and adds them to the output list : (5,6). '@0Wx' goes back to the beginning of the structure, reads one byte as a number (5), and skips one. So we're now at the Beginning of "Hello" and the output list should be (5,6,5). '/a' takes the last element in the output list as a length (5), and reads that many characters: we get (5,6,"Hello").

Now for the best part. With '@1W@0Wx' we go back to pos 1, read "\006", go back to 0, read "\005" and skip one. We are at the beginning of "Hello" again, and the ouput list is (5,6,"Hello",6,5). '/x' takes the last value in the output list and skips as many bytes, so we end up just before " World" with ouput list: (5,6,"Hello",6). Another '/a' takes the last value in the output list, and reads a 6 bytes long string. We end up with (5,6,"Hello", " World").

What I learned with this exemple is that the '/' template is not necessarily lenth/type, but that /type pops the last value read, so that you may stack more than one, and then make as many /type readings. And well, that's not too much of a surprise, it was in pack's documentation all along :).

I do agree with ig though, you should avoid that 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 reply to Re^4: how to unpack a C struct with length array preceding data array by Anonymous Monk
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-23 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found