Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: how to unpack a C struct with length array preceding data array

by BrowserUk (Pope)
on May 23, 2013 at 12:11 UTC ( #1034934=note: print w/replies, xml ) Need Help??


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

Problem with that is it only works(*) where the structure has len/data len/data; but the OP has len1/len2 data1/data2.

(*Barring playing games with relative or absolute positioning per AnomalousMonk ig's reply.)


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re^2: how to unpack a C struct with length array preceding data array

Replies are listed 'Best First'.
Re^3: how to unpack a C struct with length array preceding data array
by AnomalousMonk (Canon) on May 23, 2013 at 14:03 UTC

    I'm playing games with relative or absolute positioning?!? Have you seen ig's reply? I haven't even begun to understand what's going on there, although to be fair, I haven't really had time yet to study it: I don't know the significance of the  'W' specifier, and the  '@1W@0Wx/x/a' bit looks interesting, and the whole thing is enclosed in a  '(...)' group to which no quantification seems to be applied... Hmmm, interesting...

      I'm playing games ...

      Sorry to both you & ig for the misattribution.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1034934]
help
Chatterbox?
[1nickt]: The best sign I have sign in a protest march was carried by a kid of about 10 years "If you build a wall my generation will tear it down."
[1nickt]: ... sign I have seen ...
[1nickt]: Now that one made me hopeful! First time not angry in a long time when I saw that kid.
[Discipulus]: i propend for removing: why? because we are so few that we must find i minimal common divisor, this is certainly Perl not our (anyway private) thougths. And i say this still wondering because i love a lot freedom of expression. And i say this not for roho
[1nickt]: Discipulus that was the point of my story of taking the sticker off my truck. I know there are lots of people in the world who if I knw their private beliefs I might want to argue with them. And they with me. But life cannot all be arguments!
[1nickt]: This is less than perfect ... but demanding perfection (from people or from life) is a sure way to unhappiness.
[Discipulus]: and anyway we have CB where every (democratic) opinion can be expressed
erix eat the rich!
[1nickt]: I do think it is sad that roho has received 3 downvotes for his polite request, as did I when I objected to the profanity in stonecolddevin's sig. I upvoted both him and Karl for the discussion. Way too much downvoting for inappropriate reasons here!
Discipulus learn that 'argue' has a little negative sense, he thought was a neutral sense, 'vox media'

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2017-06-22 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (519 votes). Check out past polls.